2011-10-20 120 views
1

我正在开发一个android应用程序,我必须将图像从SD卡上传到互联网。对于这个我使用MediaStore内容提供商,正从MediaStore.Images.Media.DATA列和图像路径我现在做的是从使用Bitmap.decodeFile(字符串路径路径创建方法,然后在ImageView列表中显示该位图。我正面临的问题是当我尝试去显示这个图像列表中的活动我得到这样的错误10-20 11:10:47.070:错误/ AndroidRuntime(922):java.lang.OutOfMemoryError :位图大小超过虚拟机预算访问SD卡图像

有人可以建议我解决这个问题或其他方式来实现我的目标。提前致谢。

+0

这是更好地利用图像代码的大小调整前将图像的ImageView – Pinki

+0

你的意思,我不能让你。请多关注一下这个评论。 – Rocker

+0

调整所选图像的大小。当你调整图像大小,以减少大小,以避免内存不足异常 – Pinki

回答

1

这种情况很多时候会导致存储在SD卡的图像都是高分辨率的和您的应用程序无法处理图像的大小为堆大小

使用下面的一线得到位图

Bitmap bmp = Bitmap.decodeFile(String path) 

你应该尝试比例下来到您所需的大小

newBmp = Bitmap.createScaledBitmap(bmp, 240, 320, true); 

,然后使用newBmp在您的视图中设置。

+0

感谢他的工作。 – Rocker

1

下面的代码是用来调整所选图像

Bitmap bitmap = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream()); 
      int width = bitmap.getWidth(); 
      int height = bitmap.getHeight(); 
      int newWidth = 100; 
      int newHeight = 100; 

      // calculate the scale - in this case = 0.4f 
      float scaleWidth = ((float) newWidth)/width; 
      float scaleHeight = ((float) newHeight)/height; 

      // createa matrix for the manipulation 
      Matrix matrix = new Matrix(); 
      // resize the bit map 
      matrix.postScale(scaleWidth, scaleHeight); 

      Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, 
        width, height, matrix, true); 

      ((ImageView) findViewById(R.id.ImageviewTest)).setImageBitmap(resizedBitmap); 
0
String filepath = Environment.getExternalStorageDirectory() 
      .getAbsolutePath(); 
    File file = new File(filepath, filename); 
    FileInputStream fs; 
    try { 
     fs = new FileInputStream(file); 
     BitmapFactory.Options bfOptions = new BitmapFactory.Options(); 
     /* 
     * bfOptions.inDither=false; //Disable Dithering mode 
     * bfOptions.inPurgeable=true; //Tell to gc that whether it needs 
     * free memory, the Bitmap can be cleared 
     * bfOptions.inInputShareable=true; 
     */ 
     bfOptions.inJustDecodeBounds = false; 
     bfOptions.inTempStorage = new byte[32 * 1024]; 
     Bitmap b = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, 
       bfOptions); 
     img.setImageBitmap(b); 
} 
catch() 
{ 
}