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
Use an image loading library like Picasso or Glide. Using these libraries will prevent crashes in the future.
Your app is crashing because your image size (in MB Or KB) is too large so it is not allocating space for that. So before pasting your image in drawable just reduce the size.
OR
You can add Following in application tag at Manifest.xml
android:hardwareAccelerated="false"
android:largeHeap="true"
android:allowBackup="true"
After Adding this App will not Crash.
I don't really recommend editing manifest like this
android:hardwareAccelerated="false" , android:largeHeap="true"
These options cause not smooth animation effect on your app. Moreover 'liveData' or changing your local DB(Sqlite, Room) activate slowly. It is bad for user experience.
So I recommend RESIZE bitmap
Below is the sample code
fun resizeBitmap(source: Bitmap): Bitmap {
val maxResolution = 1000 //edit 'maxResolution' to fit your need
val width = source.width
val height = source.height
var newWidth = width
var newHeight = height
val rate: Float
if (width > height) {
if (maxResolution < width) {
rate = maxResolution / width.toFloat()
newHeight = (height * rate).toInt()
newWidth = maxResolution
}
} else {
if (maxResolution < height) {
rate = maxResolution / height.toFloat()
newWidth = (width * rate).toInt()
newHeight = maxResolution
}
}
return Bitmap.createScaledBitmap(source, newWidth, newHeight, true)
}
This should work
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
mBitmapSampled = BitmapFactory.decodeFile(mCurrentPhotoPath,options);
I'm a newbie in android developing but I hope my solution helps, it works on my condition perfectly. Im using Imageview and set it's background to "src" because im trying to make a frame animation. I got the same error but when I tried to code this it worked
int ImageID = this.Resources.GetIdentifier(questionPlay[index].Image.ToLower(), "anim", PackageName);
imgView.SetImageResource(ImageID);
AnimationDrawable animation = (AnimationDrawable)imgView.Drawable;
animation.Start();
animation.Dispose();
I got below error
"E/art: Throwing OutOfMemoryError "Failed to allocate a 47251468 byte allocation with 16777120 free bytes and 23MB until OOM"
after adding android:largeHeap="true"
in AndroidManifest.xml then I rid of all the errors
<application
android:allowBackup="true"
android:icon="@mipmap/guruji"
android:label="@string/app_name"
android:supportsRtl="true"
android:largeHeap="true"
android:theme="@style/AppTheme">