15

I need to display original image in full screen in gallery form. For thumb it will be work perfectly and when I try to display that image in full screen with original source it will not be able to display. In most cases if the image resolution is greater then 2000 then it will display error bitmap too large to be uploaded into a texture android.

I want to prevent this, I have search google but not getting any answer regarding this.

Charles
  • 50,943
  • 13
  • 104
  • 142
Pratik
  • 30,639
  • 18
  • 84
  • 159

6 Answers6

17

I came across the same problem and came up with a one liner solution for this problem here:

Picasso.with(context).load(new File(path/to/File)).fit().centerCrop().into(imageView);
Community
  • 1
  • 1
Phileo99
  • 5,581
  • 2
  • 46
  • 54
  • 1
    Thank you, I was already using picasso so this was an easy fix. – Saragis Nov 07 '15 at 20:24
  • @Saragis you're welcome, glad I could help. You can also thank me by voting up my answer – Phileo99 Nov 07 '15 at 20:55
  • Why you think this solves memory issues? `fit()` and `resize()` don't prevent you from loading an image that's too large. They simply resize the image for you. – Choletski Apr 27 '18 at 09:51
8

i just created a if else function to check if the image is bigger than 1M pixels here's the sample code:

public void onActivityResult(int requestCode, int resultCode, Intent data) {

   if (resultCode == RESULT_OK) {

     if (requestCode == SELECT_PICTURE) {

        Uri selectedImageUri = data.getData();
        selectedImagePath = getPath(selectedImageUri);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        Bitmap bitmap = BitmapFactory.decodeFile(selectedImagePath);
        int height = bitmap.getHeight(), width = bitmap.getWidth();

        if (height > 1280 && width > 960){
            Bitmap imgbitmap = BitmapFactory.decodeFile(selectedImagePath, options);
            imageView.setImageBitmap(imgbitmap);

            System.out.println("Need to resize");

        }else {
            imageView.setImageBitmap(bitmap);
            System.out.println("WORKS");
        }
Gilbert92
  • 230
  • 2
  • 10
4

Google provided a training how to do that. Download the sample from Displaying Bitmaps Efficiently

Take a look to ImageResizer class. ImageResizer.decodeSampledBitmapFrom* use this method to get downscaled image.

Minas
  • 1,422
  • 16
  • 29
3

This is the code I used to rectify my problem of fitting an image of size 3120x4196 resolution in an image view of 4096x4096 resolution. Here ImageViewId is the id of the image view created in the main layout and ImageFileLocation is the path of the image which is to be resized.

        ImageView imageView=(ImageView)findViewById(R.id.ImageViewId);          
        Bitmap d=BitmapFactory.decodeFile(ImageFileLcation);
        int newHeight = (int) ( d.getHeight() * (512.0 / d.getWidth()) );
        Bitmap putImage = Bitmap.createScaledBitmap(d, 512, newHeight, true);
        imageView.setImageBitmap(putImage);
Kartik Nigam
  • 405
  • 3
  • 5
1

You don't need to load the whole image, cause it's too large and probably your phone won't able to show the full bitmap pixels. You need to scale it first according to your device screen size. This is the best method that I found and it works pretty good: Android: Resize a large bitmap file to scaled output file

Community
  • 1
  • 1
shem
  • 4,686
  • 2
  • 32
  • 43
1

I found a way to do this without using any external libraries:

if (bitmap.getHeight() > GL10.GL_MAX_TEXTURE_SIZE) {

    // this is the case when the bitmap fails to load
    float aspect_ratio = ((float)bitmap.getHeight())/((float)bitmap.getWidth());
    Bitmap scaledBitmap = Bitmap.createBitmap(bitmap, 0, 0,
                    (int) ((GL10.GL_MAX_TEXTURE_SIZE*0.9)*aspect_ratio),
                    (int) (GL10.GL_MAX_TEXTURE_SIZE*0.9));
    imageView.setImageBitmap(scaledBitmap);
}
else{

    // for bitmaps with dimensions that lie within the limits, load the image normally
    if (Build.VERSION.SDK_INT >= 16) {
        BitmapDrawable ob = new BitmapDrawable(getResources(), bitmap);
        imageView.setBackground(ob);
    } else {
        imageView.setImageBitmap(bitmap);
    }
}

Basically, the maximum image dimensions are a system-imposed limit. The above approach will correctly resize bitmaps that exceed this limit. However, only a portion of the entire image will be loaded. To change the region displayed, you can alter the x and y parameters of the createBitmap() method.

This approach handles bitmaps of any size, including pictures taken with professional cameras.

References:

Android Universal Image Loader.

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Could please tell how we can handle the case when bitmap.getWidth() > GL10.GL_MAX_TEXTURE_SIZE ? – Othmane Sep 14 '16 at 07:44