Load huge image from file

318 views Asked by At

I'm using pictures which are stored on the Environment.getDataDirectory() and they are 1920 width and 1080 high and I want to load them in a Image View with this code:

File imgFile = new File(Environment.getDataDirectory() + "/data/com.My.Package/files/Pictures/Sun.jpg");                
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
                zoomImage.setImageBitmap(myBitmap);

But on Android 6.0 (Samsung Galaxy S6 Edge) I'm getting this error, on Android 5 (HTC One) it's working pretty well:

W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (3240x5760, max=4096x4096)

So now to the Question: Is there a way to avoid this error and without a quality drop (I will need this quality, because you can zoom the Image)

3

There are 3 answers

6
Atiq On BEST ANSWER

I think you should try loading it with Glide.

The procedure is pretty straight forward and its fast and there won't be any OOM hopefully

Just compile this in your build.gradle

 compile 'com.github.bumptech.glide:glide:3.7.0'

and then

Glide
    .with(this)
    .load(imgFile)
    .into(zoomImage);

and that's it, you will never face OOM again hopefully, I worked with images in my many apps and I have never faced OOM with it.

Hope it helps

0
Liem Vo On
7
AndroidEnthusiast On

As your error points out, your image is too large to load.

Basically right now you are loading the whole image into memory.

You have to get a smaller /scaled down version of the bitmap.

You can easily do this using BitmapFactory.Options,

Here is a link which goes into detail on the recommended technique to use for this: developer.android.com/training/displaying-bitmaps/load-bitmap.html