Android java.lang.OutOfMemoryError?

前端 未结 8 786
旧巷少年郎
旧巷少年郎 2020-12-08 16:24
04-25 08:19:10.111    2431-2603/com.example.francesco.guidedautorewithtabs E/art﹕ Throwing OutOfMemoryError \"Failed to allocate a 4194316 byte allocation with 19836         


        
相关标签:
8条回答
  • 2020-12-08 16:51

    Try this may help you add this tag in your manifest file.

    <application android:largeHeap="true">
    </application>
    

    it will allocate large heap for your app

    0 讨论(0)
  • 2020-12-08 16:56

    OutOfMemoryError is the most common problem occured in android while especially dealing with bitmaps. This error is thrown by the Java Virtual Machine (JVM) when an object cannot be allocated due to lack of memory space and also, the garbage collector cannot free some space.

    As mentioned by Aleksey, you can add below entities in your manifest file android:hardwareAccelerated="false" , android:largeHeap="true" it will work for some environment's.

    <application
        android:allowBackup="true"
        android:hardwareAccelerated="false"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
    
    0 讨论(0)
  • 2020-12-08 17:00

    This is because of low memory, size of your image file is large, to solve this problem add this method in your class:

    public static Bitmap decodeImageFile(File f,int WIDTH,int HIGHT){
                 try {
                     //Decode image size
                     BitmapFactory.Options o = new BitmapFactory.Options();
                     o.inJustDecodeBounds = true;
                     BitmapFactory.decodeStream(new FileInputStream(f),null,o);
    
                     //The new size we want to scale to
                     final int REQUIRED_WIDTH=WIDTH;
                     final int REQUIRED_HIGHT=HIGHT;
                     //Find the correct scale value. It should be the power of 2.
                     int scale=1;
                     while(o.outWidth/scale/2>=REQUIRED_WIDTH && o.outHeight/scale/2>=REQUIRED_HIGHT)
                         scale*=2;
    
                     //Decode with inSampleSize
                     BitmapFactory.Options o2 = new BitmapFactory.Options();
                     o2.inSampleSize=scale;
                     return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
                 } catch (FileNotFoundException e) {}
                 return null;
                }
    

    Then call this method where you using this:

    Bitmap b = decodeFile(f);
    

    use this insted:

    Bitmap b = decodeImageFile(f, 1280, 720);
    
    0 讨论(0)
  • 2020-12-08 17:00

    Add this tag in your manifest file.

    <application android:largeHeap="true"> </application>

    0 讨论(0)
  • 2020-12-08 17:01

    Loading Large Bitmaps Efficiently : http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    Caching Bitmaps: http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

    Try to resize the image and recycle bitmap also after usage.

    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    
     public class ImageResizer {
    
     public static Bitmap decodeSampledBitmapFromFile(String filename,
        int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options
            options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filename, options);
    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeFile(filename, options);
     }
    
       public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // BEGIN_INCLUDE (calculate_sample_size)
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    
    if (height > reqHeight || width > reqWidth) {
    
        final int halfHeight = height / 2;
        final int halfWidth = width / 2;
    
        // Calculate the largest inSampleSize value that is a power of 2 and keeps both
        // height and width larger than the requested height and width.
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    
        // This offers some additional logic in case the image has a strange
        // aspect ratio. For example, a panorama may have a much larger
        // width than height. In these cases the total pixels might still
        // end up being too large to fit comfortably in memory, so we should
        // be more aggressive with sample down the image (=larger inSampleSize).
    
        long totalPixels = width * height / inSampleSize;
    
        // Anything more than 2x the requested pixels we'll sample down further
        final long totalReqPixelsCap = reqWidth * reqHeight * 2;
    
        while (totalPixels > totalReqPixelsCap) {
            inSampleSize *= 2;
            totalPixels /= 2;
        }
    }
    return inSampleSize;
    // END_INCLUDE (calculate_sample_size)
     }
     }
    
    0 讨论(0)
  • 2020-12-08 17:06

    You can try this code....

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inSampleSize = 8;    
    
    Bitmap bm = BitmapFactory.decodeFile(img,options);
    imageView.setImageBitmap(bm);
    
    0 讨论(0)
提交回复
热议问题