{"id":1358,"date":"2016-03-03T11:46:24","date_gmt":"2016-03-03T03:46:24","guid":{"rendered":"https:\/\/ai.zhousir.top\/?p=1358"},"modified":"2016-03-03T15:23:00","modified_gmt":"2016-03-03T07:23:00","slug":"android%e5%86%85%e5%ad%98%e6%b3%84%e6%bc%8f%e7%bb%88%e6%9e%81%e8%a7%a3%e5%86%b3%e7%af%87","status":"publish","type":"post","link":"https:\/\/ai.zhousir.top\/?p=1358","title":{"rendered":"\u5185\u5b58\u6cc4\u6f0f\u63a2\u8ba8\u4e2d(Android\u7bc7)"},"content":{"rendered":"<p><a href=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/02\/640.jpg\" rel=\"fancybox\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-1370\" src=\"https:\/\/ai.zhousir.top\/wp-content\/uploads\/2016\/02\/640.jpg\" alt=\"640\" width=\"640\" height=\"173\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\u5df2\u7ecf\u4e0d\u9700\u8981\u518d\u4f7f\u7528\u4e86\uff0c\u4f46\u662f\u56e0\u4e3a\u5176\u5b83\u7684\u5bf9\u8c61\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5bfc\u81f4\u5b83\u7684\u5185\u5b58\u4e0d\u80fd\u88ab\u56de\u6536\u3002\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\u7684\u6162\u6162\u79ef\u7d2f\uff0c\u6700\u7ec8\u4f1a\u5bfc\u81f4OOM &#8220;\u5185\u5b58\u6ea2\u51fa&#8221;\u7684\u53d1\u751f\uff0c\u5343\u91cc\u4e4b\u5824\uff0c\u6bc1\u4e8e\u8681\u7a74\u3002\u6240\u4ee5\u5728\u5199\u4ee3\u7801\u7684\u8fc7\u7a0b\u4e2d\uff0c\u5e94\u8be5\u8981\u6ce8\u610f\u89c4\u907f\u4f1a\u5bfc\u81f4\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\u7684\u4ee3\u7801\u5199\u6cd5\uff0c\u63d0\u9ad8\u8f6f\u4ef6\u7684\u5065\u58ee\u6027\u3002 <!--more--><\/p>\n<p><span style=\"font-size: 14pt;\"><strong>\u4e00\u3001\u5e38\u89c1\u5bfc\u81f4\u201c\u5185\u5b58\u6cc4\u6f0f\u201d\u7684\u4ee3\u7801\u5199\u6cd5\u53ca\u89e3\u51b3\u65b9\u6848<\/strong><\/span><\/p>\n<p><strong>1.\u9759\u6001\u53d8\u91cf\u5f15\u8d77\u7684\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p>\u5728java\u4e2d\u9759\u6001\u53d8\u91cf\u7684\u751f\u547d\u5468\u671f\u662f\u5728\u7c7b\u52a0\u8f7d\u65f6\u5f00\u59cb\uff0c\u7c7b\u5378\u8f7d\u65f6\u7ed3\u675f\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u5728android\u4e2d\u5176\u751f\u547d\u5468\u671f\u662f\u5728\u8fdb\u7a0b\u542f\u52a8\u65f6\u5f00\u59cb\uff0c\u8fdb\u7a0b\u6b7b\u4ea1\u65f6\u7ed3\u675f\u3002\u6240\u4ee5\u5728\u7a0b\u5e8f\u7684\u8fd0\u884c\u671f\u95f4\uff0c\u5982\u679c\u8fdb\u7a0b\u6ca1\u6709\u88ab\u6740\u6b7b\uff0c\u9759\u6001\u53d8\u91cf\u5c31\u4f1a\u4e00\u76f4\u5b58\u5728\uff0c\u4e0d\u4f1a\u88ab\u56de\u6536\u6389\u3002\u5982\u679c\u9759\u6001\u53d8\u91cf\u5f3a\u5f15\u7528\u4e86\u67d0\u4e2aActivity\u4e2d\u53d8\u91cf\uff0c\u90a3\u4e48\u8fd9\u4e2aActivity\u5c31\u540c\u6837\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e,\u5373\u4fbf\u662f\u8be5Activity\u6267\u884c\u4e86onDestroy(\u4e0d\u8981\u5c06\u6267\u884conDestroy\u548c\u88ab\u56de\u6536\u5212\u7b49\u53f7)\u3002<\/p>\n<p>\u8fd9\u7c7b\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u4e3a\uff1a1.\u5bfb\u627e\u4e0e\u8be5\u9759\u6001\u53d8\u91cf\u751f\u547d\u5468\u671f\u5dee\u4e0d\u591a\u7684\u66ff\u4ee3\u5bf9\u8c61\u30022.\u82e5\u627e\u4e0d\u5230\uff0c\u5c06\u5f3a\u5f15\u7528\u65b9\u5f0f\u6539\u6210\u5f31\u5f15\u7528\u3002<\/p>\n<p>\u6bd4\u8f83\u5178\u578b\u7684\u4f8b\u5b50\u5982\u4e0b:<br \/>\n\u5355\u4f8b\u5f15\u8d77\u7684Context\u5185\u5b58\u6cc4\u6f0f<\/p>\n<p>[java]public class IMManager {<br \/>\n  private Context context;<br \/>\n  private static IMManager mInstance;<\/p>\n<p>  public static IMManager getInstance(Context context) {<br \/>\n    if (mInstance == null) {<br \/>\n      synchronized (IMManager.class) {<br \/>\n        if (mInstance == null)<br \/>\n          mInstance = new IMManager(context);<br \/>\n      }<br \/>\n    }<br \/>\n    return mInstance;<br \/>\n  }<\/p>\n<p>  private IMManager(Context context) {<br \/>\n    this.context = context;<br \/>\n  }<\/p>\n<p>}[\/java]<\/p>\n<p>\u5f53\u8c03\u7528getInstance\u65f6\uff0c\u5982\u679c\u4f20\u5165\u7684context\u662fActivity\u7684context\u3002\u53ea\u8981\u8fd9\u4e2a\u5355\u4f8b\u6ca1\u6709\u88ab\u91ca\u653e\uff0c\u8fd9\u4e2aActivity\u4e5f\u4e0d\u4f1a\u88ab\u91ca\u653e\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848<\/p>\n<p>\u4f20\u5165Application\u7684context,\u56e0\u4e3aApplication\u7684context\u7684\u751f\u547d\u5468\u671f\u6bd4Activity\u957f\uff0c\u53ef\u4ee5\u7406\u89e3\u4e3aApplication\u7684context\u4e0e\u5355\u4f8b\u7684\u751f\u547d\u5468\u671f\u4e00\u6837\u957f\uff0c\u4f20\u5165\u5b83\u662f\u6700\u5408\u9002\u7684\u3002<\/p>\n<p>[java]public class IMManager {<br \/>\n  private Context context;<br \/>\n  private static IMManager mInstance;<\/p>\n<p>  public static IMManager getInstance(Context context) {<br \/>\n    if (mInstance == null) {<br \/>\n      synchronized (IMManager.class) {<br \/>\n        if (mInstance == null)<br \/>\n          \/\/\u5c06\u4f20\u5165\u7684context\u8f6c\u6362\u6210Application\u7684context<br \/>\n          mInstance = new IMManager(context.getApplicationContext());<br \/>\n      }<br \/>\n    }<br \/>\n    return mInstance;<br \/>\n  }<\/p>\n<p>  private IMManager(Context context) {<br \/>\n    this.context = context;<br \/>\n  }<\/p>\n<p>}[\/java]<\/p>\n<p><strong>2\u3001\u975e\u9759\u6001\u5185\u90e8\u7c7b\u5f15\u8d77\u7684\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p>\u5728java\u4e2d\uff0c\u521b\u5efa\u4e00\u4e2a\u975e\u9759\u6001\u7684\u5185\u90e8\u7c7b\u5b9e\u4f8b\uff0c\u5c31\u4f1a\u5f15\u7528\u5b83\u7684\u5916\u56f4\u5b9e\u4f8b\u3002\u5982\u679c\u8fd9\u4e2a\u975e\u9759\u6001\u5185\u90e8\u7c7b\u5b9e\u4f8b\u505a\u4e86\u4e00\u4e9b\u8017\u65f6\u7684\u64cd\u4f5c\uff0c\u5c31\u4f1a\u9020\u6210\u5916\u56f4\u5bf9\u8c61\u4e0d\u4f1a\u88ab\u56de\u6536\uff0c\u4ece\u800c\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>\u8fd9\u7c7b\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\u4e3a\uff1a1.\u5c06\u5185\u90e8\u7c7b\u53d8\u6210\u9759\u6001\u5185\u90e8\u7c7b 2.\u5982\u679c\u6709\u5f3a\u5f15\u7528Activity\u4e2d\u7684\u5c5e\u6027\uff0c\u5219\u5c06\u8be5\u5c5e\u6027\u7684\u5f15\u7528\u65b9\u5f0f\u6539\u4e3a\u5f31\u5f15\u7528\u30023.\u5728\u4e1a\u52a1\u5141\u8bb8\u7684\u60c5\u51b5\u4e0b\uff0c\u5f53Activity\u6267\u884conDestory\u65f6\uff0c\u7ed3\u675f\u8fd9\u4e9b\u8017\u65f6\u4efb\u52a1\u3002<\/p>\n<p><strong>\u5185\u90e8\u7ebf\u7a0b\u9020\u6210\u7684\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p>[java]<br \/>\npublic class LeakAty extends Activity {<\/p>\n<p>  @Override<br \/>\n  protected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.aty_leak);<br \/>\n    test();<br \/>\n  }<\/p>\n<p>  public void test() {<br \/>\n    \/\/\u533f\u540d\u5185\u90e8\u7c7b\u4f1a\u5f15\u7528\u5176\u5916\u56f4\u5b9e\u4f8bLeakAty.this,\u6240\u4ee5\u4f1a\u5bfc\u81f4\u5185\u5b58\u6cc4\u6f0f<br \/>\n    new Thread(new Runnable() {<\/p>\n<p>      @Override<br \/>\n      public void run() {<br \/>\n        while (true) {<br \/>\n          try {<br \/>\n            Thread.sleep(1000);<br \/>\n          } catch (InterruptedException e) {<br \/>\n            e.printStackTrace();<br \/>\n          }<br \/>\n        }<br \/>\n      }<br \/>\n    }).start();<br \/>\n  }<br \/>\n  }[\/java]<\/p>\n<p>\u89e3\u51b3\u65b9\u6848<\/p>\n<p>\u5c06\u975e\u9759\u6001\u533f\u540d\u5185\u90e8\u7c7b\u4fee\u6539\u4e3a\u9759\u6001\u533f\u540d\u5185\u90e8\u7c7b<\/p>\n<p>[java]<br \/>\npublic class LeakAty extends Activity {<\/p>\n<p>  @Override<br \/>\n  protected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.aty_leak);<br \/>\n    test();<br \/>\n  }<br \/>\n  \/\/\u52a0\u4e0astatic\uff0c\u53d8\u6210\u9759\u6001\u533f\u540d\u5185\u90e8\u7c7b<br \/>\n  public static void test() {<br \/>\n    new Thread(new Runnable() {<\/p>\n<p>      @Override<br \/>\n      public void run() {<br \/>\n        while (true) {<br \/>\n          try {<br \/>\n            Thread.sleep(1000);<br \/>\n          } catch (InterruptedException e) {<br \/>\n            e.printStackTrace();<br \/>\n          }<br \/>\n        }<br \/>\n      }<br \/>\n    }).start();<br \/>\n  }<br \/>\n}[\/java]<\/p>\n<p><strong>Handler\u5f15\u8d77\u7684\u5185\u5b58\u6cc4\u6f0f<\/strong><\/p>\n<p>[java]<br \/>\npublic class LeakAty extends Activity {<\/p>\n<p>  @Override<br \/>\n  protected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.aty_leak);<br \/>\n    fetchData();<\/p>\n<p>  }<\/p>\n<p>  private Handler mHandler = new Handler() {<br \/>\n    public void handleMessage(android.os.Message msg) {<br \/>\n      switch (msg.what) {<br \/>\n      case 0:<br \/>\n        \/\/ \u5237\u65b0\u6570\u636e<br \/>\n        break;<br \/>\n      default:<br \/>\n        break;<br \/>\n      }<\/p>\n<p>    };<br \/>\n  };<\/p>\n<p>  private void fetchData() {<br \/>\n    \/\/\u83b7\u53d6\u6570\u636e<br \/>\n    mHandler.sendEmptyMessage(0);<br \/>\n  }<br \/>\n}[\/java]<\/p>\n<p>mHandler \u4e3a\u533f\u540d\u5185\u90e8\u7c7b\u5b9e\u4f8b\uff0c\u4f1a\u5f15\u7528\u5916\u56f4\u5bf9\u8c61LeakAty.this,\u5982\u679c\u8be5Handler\u5728Activity\u9000\u51fa\u65f6\u4f9d\u7136\u8fd8\u6709\u6d88\u606f\u9700\u8981\u5904\u7406\uff0c\u90a3\u4e48\u8fd9\u4e2aActivity\u5c31\u4e0d\u4f1a\u88ab\u56de\u6536\u3002<\/p>\n<p>\u89e3\u51b3\u65b9\u6848<\/p>\n<p>[java]<br \/>\npublic class LeakAty extends Activity {<br \/>\n  private TextView tvResult;<br \/>\n  private MyHandler handler;<\/p>\n<p>  @Override<br \/>\n  protected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.aty_leak);<br \/>\n    tvResult = (TextView) findViewById(R.id.tvResult);<br \/>\n    handler = new MyHandler(this);<br \/>\n    fetchData();<\/p>\n<p>  }<br \/>\n  \/\/\u7b2c\u4e00\u6b65\uff0c\u5c06Handler\u6539\u6210\u9759\u6001\u5185\u90e8\u7c7b\u3002<br \/>\n  private static class MyHandler extends Handler {<br \/>\n    \/\/\u7b2c\u4e8c\u6b65\uff0c\u5c06\u9700\u8981\u5f15\u7528Activity\u7684\u5730\u65b9\uff0c\u6539\u6210\u5f31\u5f15\u7528\u3002<br \/>\n    private WeakReference&lt;LeakAty&gt; atyInstance;<br \/>\n    public MyHandler(LeakAty aty) {<br \/>\n      this.atyInstance = new WeakReference&lt;LeakAty&gt;(aty);<br \/>\n    }<\/p>\n<p>    @Override<br \/>\n    public void handleMessage(Message msg) {<br \/>\n      super.handleMessage(msg);<br \/>\n      LeakAty aty = atyInstance == null ? null : atyInstance.get();<br \/>\n      \/\/\u5982\u679cActivity\u88ab\u91ca\u653e\u56de\u6536\u4e86\uff0c\u5219\u4e0d\u5904\u7406\u8fd9\u4e9b\u6d88\u606f<br \/>\n      if (aty == null||aty.isFinishing()) {<br \/>\n        return;<br \/>\n      }<br \/>\n      aty.tvResult.setText(&quot;fetch data success&quot;);<br \/>\n    }<br \/>\n  }<\/p>\n<p>  private void fetchData() {<br \/>\n    \/\/ \u83b7\u53d6\u6570\u636e<br \/>\n    handler.sendEmptyMessage(0);<br \/>\n  }<\/p>\n<p>  @Override<br \/>\n  protected void onDestroy() {<br \/>\n    \/\/\u7b2c\u4e09\u6b65\uff0c\u5728Activity\u9000\u51fa\u7684\u65f6\u5019\u79fb\u9664\u56de\u8c03<br \/>\n    super.onDestroy();<br \/>\n    handler.removeCallbacksAndMessages(null);<br \/>\n  }<br \/>\n}[\/java]<\/p>\n<p><strong>3\u3001\u8d44\u6e90\u672a\u5173\u95ed\u5f15\u8d77\u7684\u5185\u5b58\u6cc4\u6f0f<\/strong><br \/>\n\u5f53\u4f7f\u7528\u4e86BraodcastReceiver\u3001File\u3001Cursor\u3001Bitmap\u7b49\u8d44\u6e90\u65f6\uff0c\u5f53\u4e0d\u9700\u8981\u4f7f\u7528\u65f6\uff0c\u9700\u8981\u53ca\u65f6\u91ca\u653e\u6389\uff0c\u82e5\u6ca1\u6709\u91ca\u653e\uff0c\u5219\u4f1a\u5f15\u8d77\u5185\u5b58\u6cc4\u6f0f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>4\u3001\u6784\u9020Adapter\u65f6,\u6ca1\u6709\u4f7f\u7528\u7f13\u5b58\u7684 convertView<\/strong><\/p>\n<p>\u4ee5\u6784\u9020ListView\u7684BaseAdapter\u4e3a\u4f8b\uff0c\u5728BaseAdapter\u4e2d\u63d0\u5171\u4e86\u65b9\u6cd5\uff1a<\/p>\n<p>public View getView(intposition, View convertView, ViewGroup parent)<\/p>\n<p>\u6765\u5411ListView\u63d0\u4f9b\u6bcf\u4e00\u4e2aitem\u6240\u9700\u8981\u7684view\u5bf9\u8c61\u3002\u521d\u59cb\u65f6ListView\u4f1a\u4eceBaseAdapter\u4e2d\u6839\u636e\u5f53\u524d\u7684\u5c4f\u5e55\u5e03\u5c40\u5b9e\u4f8b\u5316\u4e00\u5b9a\u6570\u91cf\u7684view\u5bf9\u8c61\uff0c\u540c\u65f6ListView\u4f1a\u5c06\u8fd9\u4e9bview\u5bf9\u8c61\u7f13\u5b58\u8d77\u6765\u3002\u5f53\u5411\u4e0a\u6eda\u52a8ListView\u65f6\uff0c\u539f\u5148\u4f4d\u4e8e\u6700\u4e0a\u9762\u7684list item\u7684view\u5bf9\u8c61\u4f1a\u88ab\u56de\u6536\uff0c\u7136\u540e\u88ab\u7528\u6765\u6784\u9020\u65b0\u51fa\u73b0\u7684\u6700\u4e0b\u9762\u7684list item\u3002\u8fd9\u4e2a\u6784\u9020\u8fc7\u7a0b\u5c31\u662f\u7531getView()\u65b9\u6cd5\u5b8c\u6210\u7684\uff0cgetView()\u7684\u7b2c\u4e8c\u4e2a\u5f62\u53c2 View convertView\u5c31\u662f\u88ab\u7f13\u5b58\u8d77\u6765\u7684list item\u7684view\u5bf9\u8c61(\u521d\u59cb\u5316\u65f6\u7f13\u5b58\u4e2d\u6ca1\u6709view\u5bf9\u8c61\u5219convertView\u662fnull)\u3002<\/p>\n<p>\u7531\u6b64\u53ef\u4ee5\u770b\u51fa\uff0c\u5982\u679c\u6211\u4eec\u4e0d\u53bb\u4f7f\u7528convertView\uff0c\u800c\u662f\u6bcf\u6b21\u90fd\u5728getView()\u4e2d\u91cd\u65b0\u5b9e\u4f8b\u5316\u4e00\u4e2aView\u5bf9\u8c61\u7684\u8bdd\uff0c\u5373\u6d6a\u8d39\u65f6\u95f4\uff0c\u4e5f\u9020\u6210\u5185\u5b58\u5783\u573e\uff0c\u7ed9\u5783\u573e\u56de\u6536\u589e\u52a0\u538b\u529b\uff0c\u5982\u679c\u5783\u573e\u56de\u6536\u6765\u4e0d\u53ca\u7684\u8bdd\uff0c\u865a\u62df\u673a\u5c06\u4e0d\u5f97\u4e0d\u7ed9\u8be5\u5e94\u7528\u8fdb\u7a0b\u5206\u914d\u66f4\u591a\u7684\u5185\u5b58\uff0c\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5f00\u652f\u3002<\/p>\n<p>&nbsp;<\/p>\n<p><strong>5\u3001\u4e00\u4e9b\u4e0d\u826f\u4ee3\u7801\u6210\u5185\u5b58\u538b\u529b<\/strong><\/p>\n<p>&nbsp;<\/p>\n<p>\u6709\u4e9b\u4ee3\u7801\u5e76\u4e0d\u9020\u6210\u5185\u5b58\u6cc4\u9732\uff0c\u4f46\u662f\u5b83\u4eec\u6216\u662f\u5bf9\u6ca1\u4f7f\u7528\u7684\u5185\u5b58\u6ca1\u8fdb\u884c\u6709\u6548\u53ca\u65f6\u7684\u91ca\u653e\uff0c\u6216\u662f\u6ca1\u6709\u6709\u6548\u7684\u5229\u7528\u5df2\u6709\u7684\u5bf9\u8c61\u800c\u662f\u9891\u7e41\u7684\u7533\u8bf7\u65b0\u5185\u5b58\uff0c\u5bf9\u5185\u5b58\u7684\u56de\u6536\u548c\u5206\u914d\u9020\u6210\u5f88\u5927\u5f71\u54cd\u7684\uff0c\u5bb9\u6613\u8feb\u4f7f\u865a\u62df\u673a\u4e0d\u5f97\u4e0d\u7ed9\u8be5\u5e94\u7528\u8fdb\u7a0b\u5206\u914d\u66f4\u591a\u7684\u5185\u5b58\uff0c\u589e\u52a0vm\u7684\u8d1f\u62c5\uff0c\u9020\u6210\u4e0d\u5fc5\u8981\u7684\u5185\u5b58\u5f00\u652f\u3002<\/p>\n<p>\u5982Bitmap\u4f7f\u7528\u4e0d\u5f53<\/p>\n<p>\u7b2c\u4e00\u3001\u53ca\u65f6\u7684\u9500\u6bc1\u3002<\/p>\n<p>\u867d\u7136\uff0c\u7cfb\u7edf\u80fd\u591f\u786e\u8ba4Bitmap\u5206\u914d\u7684\u5185\u5b58\u6700\u7ec8\u4f1a\u88ab\u9500\u6bc1\uff0c\u4f46\u662f\u7531\u4e8e\u5b83\u5360\u7528\u7684\u5185\u5b58\u8fc7\u591a\uff0c\u6240\u4ee5\u5f88\u53ef\u80fd\u4f1a\u8d85\u8fc7Java\u5806\u7684\u9650\u5236\u3002\u56e0\u6b64\uff0c\u5728\u7528\u5b8cBitmap\u65f6\uff0c\u8981\u53ca\u65f6\u7684recycle\u6389\u3002recycle\u5e76\u4e0d\u80fd\u786e\u5b9a\u7acb\u5373\u5c31\u4f1a\u5c06Bitmap\u91ca\u653e\u6389\uff0c\u4f46\u662f\u4f1a\u7ed9\u865a\u62df\u673a\u4e00\u4e2a\u6697\u793a\uff1a\u201c\u8be5\u56fe\u7247\u53ef\u4ee5\u91ca\u653e\u4e86\u201d\u3002<\/p>\n<p>\u7b2c\u4e8c\u3001\u8bbe\u7f6e\u4e00\u5b9a\u7684\u91c7\u6837\u7387\u3002<\/p>\n<p>\u6709\u65f6\u5019\uff0c\u6211\u4eec\u8981\u663e\u793a\u7684\u533a\u57df\u5f88\u5c0f\uff0c\u6ca1\u6709\u5fc5\u8981\u5c06\u6574\u4e2a\u56fe\u7247\u90fd\u52a0\u8f7d\u51fa\u6765\uff0c\u800c\u53ea\u9700\u8981\u8bb0\u8f7d\u4e00\u4e2a\u7f29\u5c0f\u8fc7\u7684\u56fe\u7247\uff0c\u8fd9\u65f6\u5019\u53ef\u4ee5\u8bbe\u7f6e\u4e00\u5b9a\u7684\u91c7\u6837\u7387\uff0c\u90a3\u4e48\u5c31\u53ef\u4ee5\u5927\u5927\u51cf\u5c0f\u5360\u7528\u7684\u5185\u5b58\u3002\u5982\u4e0b\u9762\u7684\u4ee3\u7801\uff1a<\/p>\n<p>[java]private ImageView preview;<br \/>\nBitmapFactory.Options options = newBitmapFactory.Options();<br \/>\noptions.inSampleSize = 2;\/\/\u56fe\u7247\u5bbd\u9ad8\u90fd\u4e3a\u539f\u6765\u7684\u4e8c\u5206\u4e4b\u4e00\uff0c\u5373\u56fe\u7247\u4e3a\u539f\u6765\u7684\u56db\u5206\u4e4b\u4e00<br \/>\nBitmap bitmap =BitmapFactory.decodeStream(cr.openInputStream(uri), null, options);<br \/>\npreview.setImageBitmap(bitmap);   [\/java]<\/p>\n<p>\u603b\u7ed3<\/p>\n<ul class=\" list-paddingleft-2\">\n<li>\u5bf9 Activity \u7b49\u7ec4\u4ef6\u7684\u5f15\u7528\u5e94\u8be5\u63a7\u5236\u5728 Activity \u7684\u751f\u547d\u5468\u671f\u4e4b\u5185\uff1b \u5982\u679c\u4e0d\u80fd\u5c31\u8003\u8651\u4f7f\u7528 getApplicationContext \u6216\u8005 getApplication\uff0c\u4ee5\u907f\u514d Activity \u88ab\u5916\u90e8\u957f\u751f\u547d\u5468\u671f\u7684\u5bf9\u8c61\u5f15\u7528\u800c\u6cc4\u9732\u3002<\/li>\n<li>\u5c3d\u91cf\u4e0d\u8981\u5728\u9759\u6001\u53d8\u91cf\u6216\u8005\u9759\u6001\u5185\u90e8\u7c7b\u4e2d\u4f7f\u7528\u975e\u9759\u6001\u5916\u90e8\u6210\u5458\u53d8\u91cf\uff08\u5305\u62eccontext )\uff0c\u5373\u4f7f\u8981\u4f7f\u7528\uff0c\u4e5f\u8981\u8003\u8651\u9002\u65f6\u628a\u5916\u90e8\u6210\u5458\u53d8\u91cf\u7f6e\u7a7a\uff1b\u4e5f\u53ef\u4ee5\u5728\u5185\u90e8\u7c7b\u4e2d\u4f7f\u7528\u5f31\u5f15\u7528\u6765\u5f15\u7528\u5916\u90e8\u7c7b\u7684\u53d8\u91cf\u3002<\/li>\n<li>\u5bf9\u4e8e\u751f\u547d\u5468\u671f\u6bd4Activity\u957f\u7684\u5185\u90e8\u7c7b\u5bf9\u8c61\uff0c\u5e76\u4e14\u5185\u90e8\u7c7b\u4e2d\u4f7f\u7528\u4e86\u5916\u90e8\u7c7b\u7684\u6210\u5458\u53d8\u91cf\uff0c\u53ef\u4ee5\u8fd9\u6837\u505a\u907f\u514d\u5185\u5b58\u6cc4\u6f0f\uff1a\n<ul class=\" list-paddingleft-2\">\n<li>\u5c06\u5185\u90e8\u7c7b\u6539\u4e3a\u9759\u6001\u5185\u90e8\u7c7b<\/li>\n<li>\u9759\u6001\u5185\u90e8\u7c7b\u4e2d\u4f7f\u7528\u5f31\u5f15\u7528\u6765\u5f15\u7528\u5916\u90e8\u7c7b\u7684\u6210\u5458\u53d8\u91cf<\/li>\n<\/ul>\n<\/li>\n<li>Handler \u7684\u6301\u6709\u7684\u5f15\u7528\u5bf9\u8c61\u6700\u597d\u4f7f\u7528\u5f31\u5f15\u7528\uff0c\u8d44\u6e90\u91ca\u653e\u65f6\u4e5f\u53ef\u4ee5\u6e05\u7a7a Handler \u91cc\u9762\u7684\u6d88\u606f\u3002\u6bd4\u5982\u5728 Activity onStop \u6216\u8005 onDestroy \u7684\u65f6\u5019\uff0c\u53d6\u6d88\u6389\u8be5 Handler \u5bf9\u8c61\u7684 Message\u548c Runnable.<\/li>\n<li>\u5728 Java \u7684\u5b9e\u73b0\u8fc7\u7a0b\u4e2d\uff0c\u4e5f\u8981\u8003\u8651\u5176\u5bf9\u8c61\u91ca\u653e\uff0c\u6700\u597d\u7684\u65b9\u6cd5\u662f\u5728\u4e0d\u4f7f\u7528\u67d0\u5bf9\u8c61\u65f6\uff0c\u663e\u5f0f\u5730\u5c06\u6b64\u5bf9\u8c61\u8d4b\u503c\u4e3a null\uff0c\u6bd4\u5982\u4f7f\u7528\u5b8cBitmap \u540e\u5148\u8c03\u7528 recycle()\uff0c\u518d\u8d4b\u4e3anull,\u6e05\u7a7a\u5bf9\u56fe\u7247\u7b49\u8d44\u6e90\u6709\u76f4\u63a5\u5f15\u7528\u6216\u8005\u95f4\u63a5\u5f15\u7528\u7684\u6570\u7ec4\uff08\u4f7f\u7528 array.clear() ; array = null\uff09\u7b49\uff0c\u6700\u597d\u9075\u5faa\u8c01\u521b\u5efa\u8c01\u91ca\u653e\u7684\u539f\u5219\u3002<\/li>\n<li>\u6b63\u786e\u5173\u95ed\u8d44\u6e90\uff0c\u5bf9\u4e8e\u4f7f\u7528\u4e86BraodcastReceiver\uff0cContentObserver\uff0cFile\uff0c\u6e38\u6807 Cursor\uff0cStream\uff0cBitmap\u7b49\u8d44\u6e90\u7684\u4f7f\u7528\uff0c\u5e94\u8be5\u5728Activity\u9500\u6bc1\u65f6\u53ca\u65f6\u5173\u95ed\u6216\u8005\u6ce8\u9500\u3002<\/li>\n<li>\u4fdd\u6301\u5bf9\u5bf9\u8c61\u751f\u547d\u5468\u671f\u7684\u654f\u611f\uff0c\u7279\u522b\u6ce8\u610f\u5355\u4f8b\u3001\u9759\u6001\u5bf9\u8c61\u3001\u5168\u5c40\u6027\u96c6\u5408\u7b49\u7684\u751f\u547d\u5468\u671f\u3002<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; \u201c\u5185\u5b58\u6cc4\u6f0f\u201d\u5c31\u662f\u4e00\u4e2a\u5bf9\u8c61\u5df2\u7ecf\u4e0d\u9700\u8981\u518d\u4f7f\u7528\u4e86\uff0c\u4f46\u662f\u56e0\u4e3a\u5176\u5b83\u7684\u5bf9\u8c61\u6301\u6709\u8be5\u5bf9\u8c61\u7684\u5f15\u7528\uff0c\u5bfc\u81f4\u5b83\u7684\u5185\u5b58\u4e0d\u80fd [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[45],"tags":[77],"_links":{"self":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1358"}],"collection":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1358"}],"version-history":[{"count":5,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1358\/revisions"}],"predecessor-version":[{"id":1377,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=\/wp\/v2\/posts\/1358\/revisions\/1377"}],"wp:attachment":[{"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1358"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1358"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ai.zhousir.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1358"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}