"Bitmap too large to be uploaded into a texture"
Asked Answered
L

18

159

I'm loading a bitmap into an ImageView, and seeing this error. I gather this limit relates to a size limit for OpenGL hardware textures (2048x2048). The image I need to load is a pinch-zoom image of about 4,000 pixels high.

I've tried turning off hardware acceleration in the manifest, but no joy.

    <application
        android:hardwareAccelerated="false"
        ....
        >

Is it possible to load an image larger than 2048 pixels into an ImageView?

Lowly answered 22/4, 2012 at 18:53 Comment(10)
For anyone looking here, don't forget to put your image in a scroll view if you want it to be scrollable. That will get rid of the error. I wasted some time before realising that was my problem.Frolick
For anyone looking to display large images still keeping the image quality, refer to the library in @stoefln answer below. I used it and is worth giving a try. Definitely better than inSampleSize approach.Phototonus
For me,, working with a thumbnail of the original image is what got rid of the warning: https://mcmap.net/q/18045/-how-to-get-bitmap-from-an-uriStreptokinase
@Joe Blow current answer didn't work in your case? If no then please elaborate regarding issue you faced in context of this question?Mechanistic
@JoeBlow, Explain your specific issue. What image size, what device? etc. "This question has not received enough attention" really? After 4 years and 15 Answers and nearly 74k views?? If you are trying to give Bounty to older accepted Answer, they need to re-post (after Bounty started) to qualify.Selfindulgent
hey @Selfindulgent - it's a strange thing to get worked up about my man. I should have clicked "Reward existing answer". Nobody bothers tediously pressing the "best" button on that SO popup, because it's silly :) It's all resolved now as I clicked the bounty. Cheers!!Allege
@JoeBlow no worries. It's good to reward a deserving Answer. Didn't know if your specific issue had a unique twist... I've seen the system "eat up" rep into nothing if not awarded correctly. Didn't know about any SO pop-up (have not yet to set my own bounty)... All good.Selfindulgent
I am sorry I clicked the wrong button and caused a timewaste! @Selfindulgent Cheers man!!Allege
I try to always pay out bounties (I don't like gathering up points). I guess, if you click to my profile, then bounties, @Selfindulgent you'll see many really great QA from SO over the years !!!!!!!!!!!!!!!Allege
Here we are, 7 years later and Bitmap loading still generates issues on the Android platform... I ended up using either Glide ou Picasso on my project since no simple solution exists in the official SDK.Simitar
C
51

All rendering is based on OpenGL, so no you can't go over this limit (GL_MAX_TEXTURE_SIZE depends on the device, but the minimum is 2048x2048, so any image lower than 2048x2048 will fit).

With such big images, if you want to zoom in out, and in a mobile, you should setup a system similar to what you see in google maps for example. With the image split in several pieces, and several definitions.

Or you could scale down the image before displaying it (see user1352407's answer on this question).

And also, be careful to which folder you put the image into, Android can automatically scale up images. Have a look at Pilot_51's answer below on this question.

Chilly answered 22/4, 2012 at 19:2 Comment(7)
If this is the case, how does the Gallery app allow the display and manipulation of images taken with the camera? 2048x2048 is only a 4MP image, and many Android phones take photos much larger than this and the Gallery app seems to have no problems.Lowly
Because GL_MAX_TEXTURE_SIZE depends on the device.Chilly
This really does not make any sense. I encountered the same problem now - with an image of 1286x835 pixels. AND: only on a Galaxy Nexus I get this error message and no image! It just seems ridiculous that a top-of-the-edge smartphone cannot display such a small image! My HTC Hero is capable of displaying that! What can I do?Metallophone
See Romain's answer here: #7429496Arteaga
@OllieC I would also like to know how the gallery apps do it. So if anyone knows, or has an example for showing large images, that would be great.Lap
@innova I guess you found out, but they probably resize it to fit the screen since the image is bigger than the screen.Yul
How do you get the real limit, without using an ImageView ?Checkerboard
S
413

This isn't a direct answer to the question (loading images >2048), but a possible solution for anyone experiencing the error.

In my case, the image was smaller than 2048 in both dimensions (1280x727 to be exact) and the issue was specifically experienced on a Galaxy Nexus. The image was in the drawable folder and none of the qualified folders. Android assumes drawables without a density qualifier are mdpi and scales them up or down for other densities, in this case scaled up 2x for xhdpi. Moving the culprit image to drawable-nodpi to prevent scaling solved the problem.

Stonebroke answered 14/2, 2013 at 14:57 Comment(5)
I was having memory issues trying to load drawables. Spent 2 hrs trying to understand why this was happening. Thanks for the indirect answer.Obedience
About three and a half months now I have been programming almost full time on Android and I just now realized this. Seems dumb to make drawable essentially a hidden drawable-mdpi folder. This also explained why my custom map markers looked awful (they were being upscaled, then downscaled).Composition
yeah what the hell! i think 99% of android programmers think "drawable" means "don't scale this".Riddle
But I'm clicking image from a camera and using setImageBitmap to set an image. What to do in this case?Cognomen
I did put the troubled image on drawable. I move it to drawable-hdpi and it works. Thanks!Schulman
A
108

I have scaled down the image in this way:

ImageView iv  = (ImageView)waypointListView.findViewById(R.id.waypoint_picker_photo);
Bitmap d = new BitmapDrawable(ctx.getResources() , w.photo.getAbsolutePath()).getBitmap();
int nh = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
Bitmap scaled = Bitmap.createScaledBitmap(d, 512, nh, true);
iv.setImageBitmap(scaled);
Acrolith answered 27/3, 2013 at 17:57 Comment(6)
thanks, the createScaledBitmap was helpful for me to be able to show a too large bitmapCoston
Solved my issue.. thanks. Actually i was taking the image from camera and displaying in the ImageView in Samsung Galaxy S4 with 4.4.2Debbee
sorry, but I cant detect what is "w"Sprang
@iGude "w.photo.getAbsolutePath()" is the image pathFluoride
Sorry, what is this ctx means?Acclamation
@AmeerSabith looks like ctx is a Context object (your running Activity for example)Bailey
C
51

All rendering is based on OpenGL, so no you can't go over this limit (GL_MAX_TEXTURE_SIZE depends on the device, but the minimum is 2048x2048, so any image lower than 2048x2048 will fit).

With such big images, if you want to zoom in out, and in a mobile, you should setup a system similar to what you see in google maps for example. With the image split in several pieces, and several definitions.

Or you could scale down the image before displaying it (see user1352407's answer on this question).

And also, be careful to which folder you put the image into, Android can automatically scale up images. Have a look at Pilot_51's answer below on this question.

Chilly answered 22/4, 2012 at 19:2 Comment(7)
If this is the case, how does the Gallery app allow the display and manipulation of images taken with the camera? 2048x2048 is only a 4MP image, and many Android phones take photos much larger than this and the Gallery app seems to have no problems.Lowly
Because GL_MAX_TEXTURE_SIZE depends on the device.Chilly
This really does not make any sense. I encountered the same problem now - with an image of 1286x835 pixels. AND: only on a Galaxy Nexus I get this error message and no image! It just seems ridiculous that a top-of-the-edge smartphone cannot display such a small image! My HTC Hero is capable of displaying that! What can I do?Metallophone
See Romain's answer here: #7429496Arteaga
@OllieC I would also like to know how the gallery apps do it. So if anyone knows, or has an example for showing large images, that would be great.Lap
@innova I guess you found out, but they probably resize it to fit the screen since the image is bigger than the screen.Yul
How do you get the real limit, without using an ImageView ?Checkerboard
P
34

Instead of spending hours upon hours trying to write/debug all this downsampling code manually, why not use Picasso? It was made for dealing with bitmaps of all types and/or sizes.

I have used this single line of code to remove my "bitmap too large...." problem:

Picasso.load(resourceId).fit().centerCrop().into(imageView);
Polka answered 21/3, 2015 at 4:23 Comment(7)
Using centerCrop() without calling resize() will result in an IllegalStateException. Library forces to call resize when center crop is used.Snarl
That makes perfect sense, because cropping implies that you are resizing the image.Polka
Fast, easy and simple solution. Not sure if it is the best, but I got what I wanted. Thank you very muchAaren
still getting the same error when used target with Picasso, for the bigger sized images :(Wilmerwilmette
Try to use version 2.5.3-SNAPSHOT, seems it's now work correctly with big imagesSplat
Awesome, +1 for pointing out that Picasso has ready-made methods for these! For future readers, note that centerCrop() isn't the only option; for my needs, .fit().centerInside() solved the problem without distorting the end result.Tormoria
I would suggest using Fresco instead of Picasso. it does caching betterToaster
A
23

Addition of the following 2 attributes in (AndroidManifest.xml) worked for me:

android:largeHeap="true"
android:hardwareAccelerated="false"
Accipiter answered 27/11, 2016 at 11:55 Comment(1)
This fixed my issue on the Samsung device. ThanksPolitian
D
16

Changing the image file to drawable-nodpi folder from drawable folder worked for me.

Dorree answered 29/12, 2015 at 12:59 Comment(1)
This will prevent Android from trying to auto-scale the image based on the device's dimensions and screen density; this is why this solution works. Android will try to auto-scale anything in the drawable folder.Keavy
P
9

I used Picasso and had the same problem. image was too large at least in on size, width or height. finally I found the solution here. you can scale the large image down according to display size and also keep the aspect ratio:

    public Point getDisplaySize(Display display) {
    Point size = new Point();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        display.getSize(size);
    } else {
        int width = display.getWidth();
        int height = display.getHeight();
        size = new Point(width, height);
    }

    return size;
}

and use this method for loading image by Picasso:

    final Point displySize = getDisplaySize(getWindowManager().getDefaultDisplay());
        final int size = (int) Math.ceil(Math.sqrt(displySize.x * displySize.y));
        Picasso.with(this)
                .load(urlSource)
                .resize(size, size)
                .centerInside()
                .into(imageViewd);

also for better performance you can download the image according to width and height of the display screen, not whole the image:

    public String reviseImageUrl(final Integer displayWidth,     final Integer displayHeight,
        final String originalImageUrl) {
    final String revisedImageUrl;

    if (displayWidth == null && displayHeight == null) {
        revisedImageUrl = originalImageUrl;
    } else {
        final Uri.Builder uriBuilder = Uri.parse(originalImageUrl).buildUpon();

        if (displayWidth != null && displayWidth > 0) {
            uriBuilder.appendQueryParameter(QUERY_KEY_DISPLAY_WIDTH, String.valueOf(displayWidth));
        }

        if (displayHeight != null && displayHeight > 0) {
            uriBuilder.appendQueryParameter(QUERY_KEY_DISPLAY_HEIGHT, String.valueOf(displayHeight));
        }

        revisedImageUrl = uriBuilder.toString();
    }

    return revisedImageUrl;
}

    final String newImageUlr = reviseImageUrl(displySize.x, displySize.y, urlSource);

and then:

    Picasso.with(this)
                .load(newImageUlr)
                .resize(size, size)
                .centerInside()
                .into(imageViewd);

EDIT: getDisplaySize()

display.getWidth()/getHeight() is deprecated. Instead of Display use DisplayMetrics.

public Point getDisplaySize(DisplayMetrics displayMetrics) {
        int width = displayMetrics.widthPixels;
        int height = displayMetrics.heightPixels;
        return new Point(width, height);
}
Portraitist answered 30/7, 2015 at 20:53 Comment(0)
B
6

BitmapRegionDecoder does the trick.

You can override onDraw(Canvas canvas), start a new Thread and decode the area visible to the user.

Bloodshot answered 18/4, 2013 at 21:45 Comment(0)
L
5

As pointed by Larcho, starting from API level 10, you can use BitmapRegionDecoder to load specific regions from an image and with that, you can accomplish to show a large image in high resolution by allocating in memory just the needed regions. I've recently developed a lib that provides the visualisation of large images with touch gesture handling. The source code and samples are available here.

Lea answered 5/1, 2014 at 15:40 Comment(0)
L
4

View level

You can disable hardware acceleration for an individual view at runtime with the following code:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

Lacee answered 5/2, 2015 at 10:51 Comment(0)
P
3

I ran through same problem, here is my solution. set the width of image same as android screen width and then scales the height

Bitmap myBitmap = BitmapFactory.decodeFile(image.getAbsolutePath());
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Log.e("Screen width ", " "+width);
Log.e("Screen height ", " "+height);
Log.e("img width ", " "+myBitmap.getWidth());
Log.e("img height ", " "+myBitmap.getHeight());
float scaleHt =(float) width/myBitmap.getWidth();
Log.e("Scaled percent ", " "+scaleHt);
Bitmap scaled = Bitmap.createScaledBitmap(myBitmap, width, (int)(myBitmap.getWidth()*scaleHt), true);
myImage.setImageBitmap(scaled);

This is better for any size android screen. let me know if it works for you.

Panne answered 17/5, 2015 at 15:28 Comment(0)
P
2

Scale down image:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;

// Set height and width in options, does not return an image and no resource taken
BitmapFactory.decodeStream(imagefile, null, options);

int pow = 0;
while (options.outHeight >> pow > reqHeight || options.outWidth >> pow > reqWidth)
    pow += 1;
options.inSampleSize = 1 << pow; 
options.inJustDecodeBounds = false;
image = BitmapFactory.decodeStream(imagefile, null, options);

The image will be scaled down at the size of reqHeight and reqWidth. As I understand inSampleSize only take in a power of 2 values.

Parlay answered 27/2, 2014 at 12:39 Comment(1)
how to know reqHeight and reqWidth? I mean do we need to fix it to static value? or we can change those w.r.t devices?Flagellant
K
2

Use Glide library instead of directly loading into imageview

Glide : https://github.com/bumptech/glide

Glide.with(this).load(Uri.parse(filelocation))).into(img_selectPassportPic);
Kudos answered 17/5, 2018 at 6:56 Comment(1)
Using Glide instead of Picasso helped. Seems like Glide handle such problems by defaultYuhas
T
1

I tried all the solutions above, one-after-the-other, for quite many hours, and none seemed to work! Finally, I decided to look around for an official example concerning capturing images with Android's camera, and displaying them. The official example (here), finally gave me the only method that worked. Below I present the solution I found in that example app:

public void setThumbnailImageAndSave(final ImageView imgView, File imgFile) {

            /* There isn't enough memory to open up more than a couple camera photos */
    /* So pre-scale the target bitmap into which the file is decoded */

    /* Get the size of the ImageView */
    int targetW = imgView.getWidth();
    int targetH = imgView.getHeight();

    /* Get the size of the image */
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    /* Figure out which way needs to be reduced less */
    int scaleFactor = 1;
    if ((targetW > 0) || (targetH > 0)) {
        scaleFactor = Math.min(photoW/targetW, photoH/targetH);
    }

    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    /* Decode the JPEG file into a Bitmap */
    Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), bmOptions);

    /* Associate the Bitmap to the ImageView */
    imgView.setImageBitmap(bitmap);
    imgView.setVisibility(View.VISIBLE);
}
Theiss answered 5/11, 2015 at 11:19 Comment(0)
C
0

NOTE FOR THOSE WHO WANT TO PUT IMAGES OF SMALL SIZE:

Pilot_51's solution (moving your images to drawable-nodpi folder) works, but has another problem: It makes images TOO SMALL on screen unless the images are resized to a very large (like 2000 x 3800) resolution to fit screen -- then it makes your app heavier.

SOLUTION: put your image files in drawable-hdpi -- It worked like a charm for me.

Cellist answered 30/6, 2016 at 18:7 Comment(2)
You are just masking your image density problem. On hardware with low density your images will look bigger.Desiredesirea
i also do not think that Pilot_51's solution has any issues, you should use appropriate image sizes as per your needsCamillacamille
C
0

Using the correct drawable subfolder solved it for me. My solution was to put my full resolution image (1920x1200) into the drawable-xhdpi folder, instead of the drawable folder.

I also put a scaled down image (1280x800) into the drawable-hdpi folder.

These two resolutions match the 2013 and 2012 Nexus 7 tablets I'm programming. I also tested the solution on some other tablets.

Clan answered 21/4, 2017 at 21:24 Comment(0)
P
0
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    ///*
    if (requestCode == PICK_FROM_FILE && resultCode == RESULT_OK && null != data){



        uri = data.getData();

        String[] prjection ={MediaStore.Images.Media.DATA};

        Cursor cursor = getContentResolver().query(uri,prjection,null,null,null);

        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(prjection[0]);

        ImagePath = cursor.getString(columnIndex);

        cursor.close();

        FixBitmap = BitmapFactory.decodeFile(ImagePath);

        ShowSelectedImage = (ImageView)findViewById(R.id.imageView);

      //  FixBitmap = new BitmapDrawable(ImagePath);
        int nh = (int) ( FixBitmap.getHeight() * (512.0 / FixBitmap.getWidth()) );
        FixBitmap = Bitmap.createScaledBitmap(FixBitmap, 512, nh, true);

       // ShowSelectedImage.setImageBitmap(BitmapFactory.decodeFile(ImagePath));

        ShowSelectedImage.setImageBitmap(FixBitmap);

    }
}

This code is work

Panslavism answered 16/7, 2017 at 7:19 Comment(0)
C
0

Add this code in manifest

<activity
        android:name="com.yalantis.ucrop.UCropActivity"
        android:screenOrientation="portrait"
        android:hardwareAccelerated="false"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar" />
Caller answered 16/8, 2023 at 8:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.