2012-08-02 63 views
1

我遇到內存不足的問題在我的應用程序 和一些搜索之後,我發現這個代碼Android的內存溢出問題

//decodes image and scales it to reduce memory consumption 
private Bitmap decodeFile(File f){ 
    try { 
     //Decode image size 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

     //The new size we want to scale to 
     final int REQUIRED_SIZE=70; 

     //Find the correct scale value. It should be the power of 2. 
     int scale=1; 
     while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE) 
      scale*=2; 

     //Decode with inSampleSize 
     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize=scale; 
     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
    } catch (FileNotFoundException e) {} 
    return null; 
} 

但IDK如何使用它,任何幫助嗎? 並且代碼正確嗎?

+0

你想要做什麼,你得到這個錯誤?只是在沒有任何關於該問題的描述的情況下發現的代碼實際上並沒有幫助 – tyczj 2012-08-02 20:26:30

+0

您可以發佈整個異常/ logcat錯誤消息和它發生的行嗎? – ikh 2012-08-02 20:27:43

+0

由大尺寸照片造成的錯誤,並且此代碼縮小了照片尺寸..但IDK如何使用它.. – 2012-08-02 20:45:54

回答

2

但IDK如何使用它

那我猜你還沒有實現它! 那麼,該方法會將您的文件(保存在SD卡中)轉換爲可以用作ImageView背景的調整大小的位圖。

Bitmap bitmap = decodeFile(new File(your_string_file_path)); 
myImageView.setImageBitmap(bitmap); 

這就是你要知道,我想:現在

,回答你的問題,你可以爲我告訴你下面使用它。