2010-06-14 89 views
3

我正在使用以下代码在我的ImageView中显示位图。 当我尝试加载大小比例大于1.5MB的图像时,它给我错误。 任何人建议我的解决方案?如何处理内存不足错误?

try { 

         URL aURL = new URL(myRemoteImages[val]); 
         URLConnection conn = aURL.openConnection(); 

         conn.connect(); 
         InputStream is = null; 
         try 
         { 
          is= conn.getInputStream(); 
         }catch(IOException e) 
         { 


          return 0; 

         } 
         int a= conn.getConnectTimeout(); 
         BufferedInputStream bis = new BufferedInputStream(is); 

         Bitmap bm; 
         try 
         { 
          bm = BitmapFactory.decodeStream(bis); 
         }catch(Exception ex) 
         { 
          bis.close(); 
          is.close(); 
          return 0; 
         } 
         bis.close(); 
         is.close(); 
         img.setImageBitmap(bm); 

        } catch (IOException e) { 
         return 0; 
        } 

        return 1; 

登录猫:

06-14 12:03:11.701: ERROR/AndroidRuntime(443): Uncaught handler: thread main exiting due to uncaught exception 
06-14 12:03:11.861: ERROR/AndroidRuntime(443): java.lang.OutOfMemoryError: bitmap size exceeds VM budget 
06-14 12:03:11.861: ERROR/AndroidRuntime(443):  at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 

回答

1

你应该inSampleSize选项进行解码,以减少内存消耗。 Strange out of memory issue while loading an image to a Bitmap object

另一种选择inJustDecodeBounds可以帮助你找到正确的inSampleSize价值http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

+0

为了避免java.lang.OutOfMemory例外,对其进行解码,除非你绝对信任来源为您提供了可用内存内舒适适合预见的大小的图像数据之前检查的位图的尺寸。 BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(getResources(),R.id.myimage,options); int imageHeight = options.outHeight; int imageWidth = options.outWidth; String imageType = options.outMimeType; – hitesh141 2015-04-08 05:35:39

0

总的来说,我认为这个博客介绍了如何看内存分配的最佳实践/如何使用弱/软引用,以避免溢出。 希望this helps

-2
try { 
    Bitmap bitmap=null; 

    byte[] profileImageInBytes; 

    String url="http://photo.net/learn/collage/complete-full-size.jpg"; 

    HttpGet httpRequest = null; 

    httpRequest = new HttpGet(url); 

    HttpClient httpclient = new DefaultHttpClient(); 

    HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); 

    HttpEntity entity = response.getEntity(); 

    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 

    InputStream instream = bufHttpEntity.getContent(); 

    System.gc(); 

    Runtime.getRuntime().gc(); 

    BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options(); 
    bmpFactoryOptions.inJustDecodeBounds = true; 
    bmpFactoryOptions.inTempStorage = new byte[32 * 1024]; 
    bmpFactoryOptions.inSampleSize = 4;  
    bmpFactoryOptions.outWidth = 640;  
    bmpFactoryOptions.outHeight = 480;  
    bmpFactoryOptions.inDither=false;   
    bmpFactoryOptions.inInputShareable=true; 

    bitmap = BitmapFactory.decodeStream(instream, new Rect(), bmpFactoryOptions); 

    System.out.println("hi " +bitmap); 
    Bitmap map = Bitmap.createScaledBitmap(bitmap, 200, 200, true); 
    System.out.println("23"); 
    System.out.println("hihi hi " +map); 
    BitmapDrawable bmd = new BitmapDrawable(map); 

    System.out.println("24"); 
    System.out.println("hihi hi " +bmd); 
    ByteArrayOutputStream stream = new ByteArrayOutputStream(); 

    System.out.println(stream); 

    map.compress(Bitmap.CompressFormat.JPEG, 100, stream); 

    int heightRatio = (int) Math.ceil(bmpFactoryOptions.outHeight 
    /(float) 400); 
    int widthRatio = (int) Math.ceil(bmpFactoryOptions.outWidth 
    /(float) 400); 

    if (heightRatio > 1 || widthRatio > 1) { 
    if (heightRatio > widthRatio) { 
     bmpFactoryOptions.inSampleSize = heightRatio; 
    } else { 
     bmpFactoryOptions.inSampleSize = widthRatio; 
    } 
    } 

    Bundle params=new Bundle(); 
    params.putString("method", "photos.upload"); 
    profileImageInBytes = stream.toByteArray(); 
    System.out.println(profileImageInBytes); 
    System.out.println(" profile image bytes "); 
    System.out.println("Bytes : " + profileImageInBytes); 
    params.putByteArray("picture", profileImageInBytes); 
    System.out.println("My Picture : " + params); 

    mAsyncRunner.request(null, params, "POST", 
      new SampleUploadListener(), null); 
    System.out.println("Uploading"); 

} 
catch (IOException e) { 
    e.printStackTrace(); 
} 
+0

请修正您的格式,并且您的代码几乎不可读 – 2012-11-21 11:55:30

+0

欢迎使用Stack Overflow!请不要只发布一段代码,请*解释*为什么这段代码能解决问题。没有解释,这不是一个答案。 – 2012-11-21 11:55:37