2013-10-17 42 views
1

在我的android应用程序中,我必须允许用户单击按钮打开图库并选择图像。然后需要将特定的选定图像加载到我的布局(UI)中的图像视图中。我有一些代码,但它来java.lang.outofmemory.Please任何人都可以帮助我?Android:应用程序在上传图像时在设备上崩溃,java.lang.outofMemoryError

+0

您正在加载的位图可能对测试设备上可用的内存量太大。或者,您可能一次将太多位图加载到库中。人们经常碰到这种情况,在处理Android上的位图时,必须对代码进行一些预先考虑。这里是由罗曼盖伊介绍这个话题:https://dl.google.com/io/2009/pres/Th_0230_TurboChargeYourUI-HowtomakeyourAndroidUIfastandefficient.pdf – Turnsole

+0

@Tnsnsole谢谢你的工作很好 – shakthivel

回答

3

你应该解码在onActivityResult()方法中的图像URI。 将此方法调用decodeBitmap。

/** 
    * This is very useful to overcome Memory waring issue while selecting image 
    * from Gallery 
    * 
    * @param selectedImage 
    * @param context 
    * @return Bitmap 
    * @throws FileNotFoundException 
    */ 
    public static Bitmap decodeBitmap(Uri selectedImage, Context context) 
      throws FileNotFoundException { 
     BitmapFactory.Options o = new BitmapFactory.Options(); 
     o.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(context.getContentResolver() 
       .openInputStream(selectedImage), null, o); 

     final int REQUIRED_SIZE = 100; 

     int width_tmp = o.outWidth, height_tmp = o.outHeight; 
     int scale = 1; 
     while (true) { 
      if (width_tmp/2 < REQUIRED_SIZE || height_tmp/2 < REQUIRED_SIZE) { 
       break; 
      } 
      width_tmp /= 2; 
      height_tmp /= 2; 
      scale *= 2; 
     } 

     BitmapFactory.Options o2 = new BitmapFactory.Options(); 
     o2.inSampleSize = scale; 
     return BitmapFactory.decodeStream(context.getContentResolver() 
       .openInputStream(selectedImage), null, o2); 
    } 

有关详情,请尽管话题显示位图高效

http://developer.android.com/training/displaying-bitmaps/index.html

希望这有助于。

+0

谢谢你很多..工作精细.. – shakthivel

相关问题