1 year ago

#199229

test-img

Himanshu Kansal

Android: Bitmap too large. Nothing working except setting android:hardwareAccelerated to false

I have an Android application where I need to load an image in the imageView. I have tried every recommended solution but the app is still crashing on old Redmi devices. The exception, I'm getting is trying to draw too large bitmap. The stack trace I got on robo test is the following:

java.lang.RuntimeException: Canvas: trying to draw too large(268435456bytes) bitmap.
       FATAL EXCEPTION: Thread-3
Process: com.hkprog.captiongenerator, PID: 20296

java.lang.RuntimeException: Canvas: trying to draw too large(268435456bytes) bitmap.

at android.view.DisplayListCanvas.throwIfCannotDraw(DisplayListCanvas.java:229)
at android.view.RecordingCanvas.drawBitmap(RecordingCanvas.java:97)
at android.graphics.drawable.BitmapDrawable.draw(BitmapDrawable.java:529)
at android.widget.ImageView.onDraw(ImageView.java:1367)
at android.view.View.draw(View.java:19337)
at android.view.View.updateDisplayListIfDirty(View.java:18254)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.draw(View.java:19345)
at android.view.View.updateDisplayListIfDirty(View.java:18254)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at androidx.recyclerview.widget.RecyclerView.drawChild(RecyclerView.java:5030)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.draw(View.java:19345)
at androidx.recyclerview.widget.RecyclerView.draw(RecyclerView.java:4429)
at android.view.View.updateDisplayListIfDirty(View.java:18254)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.updateDisplayListIfDirty(View.java:18245)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at androidx.coordinatorlayout.widget.CoordinatorLayout.drawChild(CoordinatorLayout.java:1277)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.draw(View.java:19345)
at android.view.View.updateDisplayListIfDirty(View.java:18254)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at androidx.fragment.app.FragmentContainerView.drawChild(FragmentContainerView.java:235)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at androidx.fragment.app.FragmentContainerView.dispatchDraw(FragmentContainerView.java:223)
at android.view.View.updateDisplayListIfDirty(View.java:18245)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at androidx.constraintlayout.widget.ConstraintLayout.dispatchDraw(ConstraintLayout.java:1975)
at android.view.View.updateDisplayListIfDirty(View.java:18245)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.updateDisplayListIfDirty(View.java:18245)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.updateDisplayListIfDirty(View.java:18245)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.updateDisplayListIfDirty(View.java:18245)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.updateDisplayListIfDirty(View.java:18245)
at android.view.View.draw(View.java:19052)
at android.view.ViewGroup.drawChild(ViewGroup.java:4301)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:4085)
at android.view.View.draw(View.java:19345)
at com.android.internal.policy.DecorView.draw(DecorView.java:792)
at android.view.View.updateDisplayListIfDirty(View.java:18254)
at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:678)
at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:684)
at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:792)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:3164)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2964)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2502)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1482)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7124)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1008)
at android.view.Choreographer.doCallbacks(Choreographer.java:804)
at android.view.Choreographer.doFrame(Choreographer.java:732)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:994)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at androidx.test.espresso.base.Interrogator.loopAndInterrogate(Interrogator.java:10)
at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:7)
at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:1)
at androidx.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:5)
at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:6)
at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:1)
at androidx.test.espresso.action.Tap.sendSingleTap(Tap.java:5)
at androidx.test.espresso.action.Tap.-$$Nest$smsendSingleTap(Unknown Source:0)
at androidx.test.espresso.action.Tap$1.sendTap(Tap.java:1)
at androidx.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:4)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:2)
at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:21)
at androidx.test.espresso.ViewInteraction.-$$Nest$mdoPerform(Unknown Source:0)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:2)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:794)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:6662)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

The app is also crashing on some devices, by loading the images into imageview from the drawable folder. The image's size in drawable folder is less than 2mb.

The only way I'm able to fix this error is by adding the following line in AndroidManifest.xml:

android:hardwareAccelerated="false"

However, this is resulting in disabling all the elevations of CardView.

Solutions, I've tried:

Loading a Scaled Down Version into Memory

public static int calculateInSampleSize (BitmapFactory.Options options,
                                         int reqWidth, int reqHeight) {

    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;

        while ((halfHeight / inSampleSize) >= reqHeight
                && (halfWidth / inSampleSize) >= reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}


Bitmap imageBitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(),
            selectedImage);
InputStream inputStream = context.getContentResolver().openInputStream(selectedImage);


final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, null, options);

options.inSampleSize = calculateInSampleSize(options, 100, 100);
options.inJustDecodeBounds = false;

BitmapFactory.decodeStream(inputStream, null, options);
imageView.setImageBitmap(imageBitmap);

Glide

Glide.with(context).load(selectedImage).apply(
        new RequestOptions().diskCacheStrategy(DiskCacheStrategy.NONE))
        .centerCrop().into(imageView);

Picasso

Picasso.get().load(selectedImage).resize(1080, 720)
        .onlyScaleDown()
        .into(imageView);

Picasso: extracting the image from the ImageView and not from URI or path

As suggested in this post

Picasso.get()
        .load(selectedImage)
        .resize(1024,1024)
        .onlyScaleDown()
        .centerCrop()
        .into(imageView, new Callback(){
            @Override
            public void onSuccess() {
                BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
                Bitmap imageBitmap = drawable.getBitmap();
                //isImageSelected = true;

                Log.d("FileSize", Formatter.formatFileSize(getActivity(),
                        imageBitmap.getByteCount()));
            }

            @Override
            public void onError(Exception e) {
                Toast.makeText(getActivity(), "Could Not Load Image", Toast.LENGTH_SHORT).show();
            }
        });

All these solutions don’t seem to work. And app keep crashing on the older devices. Is there any way I could solve this?

Thanks in advance.

android

bitmap

android-imageview

android-bitmap

android-image

0 Answers

Your Answer

Accepted video resources