2011-11-07 120 views
1

我從網絡上獲得的圖像時,圖像尺寸較大,我發現了以下錯誤如何從Android中加載url後縮小圖像大小?

java.lang.OutOfMemoryError: bitmap size exceeds VM budget 

我的代碼如下我被使用LoadImageFromWebOperatins方法加載圖像。

animation.addFrame(LoadImageFromWebOperations(feed.getItem(i).getImage()), 2500); 
animation.addFrame(LoadImageFromWebOperations(feed.getItem(i).getImage1()), 2500); animation.setOneShot(false); 

iv.setBackgroundDrawable(animation); 
iv.post(new Starter()); 
private Drawable LoadImageFromWebOperations(String url) { 
    try { 

     InputStream is = (InputStream) new URL(url).getContent(); 

     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
    } catch (Exception e) { 
     System.out.println("Exc=" + e); 
     return null; 

    } 
} 

在這種情況下,我該如何解決這個錯誤?請爲此提供解決方案。

回答

2

我做了這樣的事情:

private static final float MAX_IMAGE_SIZE = 800; 
private static final String CURRENTLY_PROCESSED_IMAGE = "currentlyProcessedImage.jpg"; 
InputStream stream; 
String filePath = null; 
try { 
    //set stream and prepare image 
    stream = getContentResolver().openInputStream(data.getData()); 
    Uri imagePath = data.getData(); 
    Bitmap realImage = BitmapFactory.decodeStream(stream); 

    //resize to 800x800 (proportionally) 
    float ratio = Math.min((float)MAX_IMAGE_SIZE/realImage.getWidth(), (float)MAX_IMAGE_SIZE/realImage.getHeight()); 
    Log.i("PixMe", "Ratio: " + String.valueOf(ratio)); 
    int width = Math.round((float)ratio * realImage.getWidth()); 
    int height = Math.round((float)ratio * realImage.getHeight()); 

    //scale down the image 
    Bitmap newBitmap = Bitmap.createScaledBitmap(realImage, width, height, true); 

    //prepare cache dir 
    filePath = getFilesDir().getAbsolutePath() 
     + File.separator + CURRENTLY_PROCESSED_IMAGE; 
    // String filePath = Environment.getExternalStorageDirectory() 
    // + File.separator + bufferPath; 
    ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 

    //save scaled down image to cache dir 
    newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); 

    File imageFile = new File(filePath); 

    // write the bytes in file 
    FileOutputStream fo = new FileOutputStream(imageFile); 
    fo.write(bytes.toByteArray()); 
} catch (FileNotFoundException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
+0

如果我保存在我的LoadImageFromWebOperations中,我收到錯誤。 –

+0

什麼錯誤?.... – ariefbayu

+0

這是返回類型是可繪製的,但通過上面的代碼,我會得到BitMap這會導致問題 –

2

您可以實現這樣的:

BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inSampleSize = 2; 
    Bitmap bit = BitmapFactory.decodeStream(inputStream,null,o); 
    Bitmap scaled = Bitmap.createScaledBitmap(bit, width, height, true); 
    bit.recycle(); 
+0

我需要使用這個代碼 –

+0

你沒有檢查過decodeStream(inputStream .....)嗎?這意味着它需要inputStream。 –

+0

我的返回類型是可繪製的BitMap將返回類型是位圖,因此在這裏出現不匹配。我將這些代碼添加到Web操作方法的Loadimage中,但在addframe中獲取錯誤。 –

0

您是否使用了大量的圖片?在這種情況下,請確保在每個ImageView的每個Bitmap上再次調用recycle()。這爲我解決了很多OOM異常。