How to deal with a memory error?

I use the following code to display a bitmap in my ImageView. When I try to upload an image that is larger than 1.5 MB, for example, this gives me an error. Does anyone offer me a solution?

  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;

Cat Log:

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)
+3
source share
3 answers

You must decode with the inSampleSize parameter to reduce memory consumption. Strange memory issue when loading image into Bitmap object

Another inJustDecodeBounds option can help you find the right inSampleSize value http://groups.google.com/group/android-developers/browse_thread/thread/bd858a63563a6d4a

+1
source

, , / Weak/Soft References, . .

0
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();
}
-2
source

Source: https://habr.com/ru/post/1749883/


All Articles