Android:java.lang.OutOfMemoryError: Failed to allocate a 23970828 byte allocation with 2097152 free bytes and 2MB until OOM

前端 未结 30 2074
暗喜
暗喜 2020-11-21 07:33

I want to show the Bitmap image in ImageView from sd card which is stored already. After run my application is crash and getting Ou

相关标签:
30条回答
  • 2020-11-21 08:08
    Last but not the least....
    
    Try Method One:
    
    Simple Add these lines of code in the gradle file
    
    dexOptions {
            incremental true
            javaMaxHeapSize "4g"
         }
    
    Example:
    
    android {
        compileSdkVersion XX
        buildToolsVersion "28.X.X"
    
        defaultConfig {
            applicationId "com.example.xxxxx"
            minSdkVersion 14
            targetSdkVersion 19
        }
    
    dexOptions {
            incremental true
            javaMaxHeapSize "4g"
         }
    }
    
    *******************************************************************
    
    Method Two:
    
    Add these two lines of code in manifest file...
    
    android:hardwareAccelerated="false" 
    android:largeHeap="true"
    
    Example:
    
    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:hardwareAccelerated="false"
            android:largeHeap="true"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".MainActivity"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
    It will Work for sure any of these cases.....
    
    0 讨论(0)
  • 2020-11-21 08:09

    I have found that images in the 'drawable' folder will get converted to a much larger image on high def phones. For example, a 200k image will be resampled to a higher res, like 800k or 32000k. I had to discover this on my own and to date have not seen documentation for this heap memory trap. To prevent this I put everything in a drawable-nodpi folder (in addition to using 'options' in BitmapFactory based on the particular device heap). I can't afford to have the size of my app bloated with multiple drawable folders, particularly as the range of screen defs is so vast now. The tricky thing is studio now doesn't specifically indicate the 'drawable-nodpi' folder as such in the project view, it just shows a 'drawable' folder. If you're not careful, when you drop an image to this folder in studio, it won't actually get dropped into the drawable-nodpi:

    Careful here 'backgroundtest' did not actually go to drawable-nodpi and will 
    be resampled higher, such as 4x or 16x of the original dimensions for high def screens.
    

    Be sure to click down to the nodpi folder in the dialogue, as the project view won't show you all drawable folders separately like it used to, so it won't be immediately apparent that it went to wrong one. Studio recreated the vanilla 'drawable' at some point for me after I had deleted it long ago:

    0 讨论(0)
  • 2020-11-21 08:09

    I suppose you want to use this image as an icon. As Android is telling you, your image is too large. What you just need to do is scale your image so that Android knows which size of the image to use and when according to screen resolution. To accomplish this, in Android Studio: 1. right click on the res folder, 2. select Image Asset 3. Select icon Type 4. Give the icon a name 5. Select Image on Asset Type 6. Trim your image Click next and finish. In your xml or source code just refer to the image which will now be located either in the layout or mipmap folder according to asset type selected. The error will go away.

    0 讨论(0)
  • 2020-11-21 08:10

    Actually you can add in your manifest these lines android:hardwareAccelerated="false" , android:largeHeap="true" it is working for some situations, but be aware that the other part of code can be arguing with this.

    <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-11-21 08:10

    If uploading an image, try reducing the image quality, which is the second parameter of the Bitmap. This was the solution in my case. Previously it was 90, then I tried with 60 (as it is in the code below now).

    Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    finalBitmap.compress(Bitmap.CompressFormat.JPEG,60,baos);
    byte[] b = baos.toByteArray();
    
    0 讨论(0)
  • 2020-11-21 08:10

    Try the simplest one. Maybe Your app is crashing because your image size (in MB) is too large so it is not allocating space for that. So before pasting your image in drawable just reduce the size by any viewer software or if you are taking image from gallery at the run time than before saving it compress your bitmap. It worked for me. definitely for u will be.

    0 讨论(0)
提交回复
热议问题