2011-11-07 195 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异常。