11

I think there is a problem with my ImageView. i created a gallery, where I can touch an image and put it in my ImageView below:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:background="@drawable/fonddegrade">

<Gallery android:layout_height="wrap_content" 
    android:id="@+id/gallery" 
    android:layout_width="fill_parent" />

<ImageView android:layout_below="@+id/gallery"
    android:layout_height="wrap_content" 
    android:id="@+id/laphoto" 
    android:layout_width="wrap_content" 
    android:layout_centerHorizontal="true"/>

This is perfectly working with small image, but not with big image (3264 * 1952). When I touch it (so, trying to put it in the ImageView), I have an error and my appplication crash. Here is my java code to display the image:

        public void onCreate(Bundle savedInstanceState) {

            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.photo);

            File images; // Trouver le bon endroit de stockage
            if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
                images = new File("/sdcard/MyPerformance/Photo");
            else
                images = this.getFilesDir();

            images.mkdirs();
            File[] imagelist = images.listFiles(new FilenameFilter(){   
                @Override   
                public boolean accept(File dir, String name)   
                {   
                    return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
                }
            });

            mFiles = new String[imagelist.length];
            for(int i = 0 ; i< imagelist.length; i++)   
            {   
                mFiles[i] = imagelist[i].getAbsolutePath();   
            }
            mUrls = new Uri[mFiles.length];
            for(int i = 0; i < mFiles.length; i++)   
            {   
                mUrls[i] = Uri.parse(mFiles[i]);      
            }

            imgView = (ImageView)findViewById(R.id.laphoto);
            if(mFiles.length != 0)
                imgView.setImageURI(mUrls[0]);

            gallery = (Gallery) findViewById(R.id.gallery);
            gallery.setAdapter(new ImageAdapter(this));

            gallery.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                    imgView.setImageURI(mUrls[position]);
                }
            });
    }

Either the problem comes from the setImageURI (but I don't think this is the cause, since it's work with small image) or because of the size of the picture.

What solution can you give me to resolve this problem? You have my thanks.

PS: Why my "Hello" are always deleted?

FR073N
  • 2,011
  • 4
  • 29
  • 42
  • So what is the problem? Does it crash? Btw, your image looks really big, Android might go out of memory while loading it. – Jarno Argillander Nov 20 '11 at 21:41
  • Pre resize the image - this one is pretty pig. A commercial product is http://www.avs4you.com/AVS-Image-Converter.aspx – mozillanerd Nov 20 '11 at 21:41
  • Also, if you just check the crash error in logcat, it will give you a clue?? Please do that, and post the error if still in doubt. – Peterdk Nov 20 '11 at 22:48
  • About your 'Hello', you can take a look here: [Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts) – Adinia Nov 21 '11 at 10:03

3 Answers3

9

Your image is probably too big for Android and it goes out of memory. Apps might have as low as 16Mb of usable memory. Your image takes 3264 * 1952 * 4 = ~25.5Mb (widthheightargb). So might be best to resize the images into smaller size.

See: http://android-developers.blogspot.co.uk/2009/01/avoiding-memory-leaks.html
Then: Strange out of memory issue while loading an image to a Bitmap object
Finally: VM running out of memory while getting images from the cache

Community
  • 1
  • 1
Jarno Argillander
  • 5,885
  • 2
  • 31
  • 33
  • 3
    This is it! Your links helped me a lot, my problem was the same as the second link, and I used the same solution to get rid of that problem. So, we have to scale too big image for good working. – FR073N Nov 21 '11 at 20:40
1

Here's a bitmap[ util class to assist you with the handling of the large bitmaps.

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class BitmapUtils {

    public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;

    }

    return inSampleSize;
}


    public static Bitmap decodeSampledBitmapFromResource(String pathToFile,
                int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(pathToFile, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(pathToFile, options);
    }

}
Jaiprakash Soni
  • 4,100
  • 5
  • 36
  • 67
cafebabe1991
  • 4,928
  • 2
  • 34
  • 42
1

With Glide library:

Glide.with(getContext())
     .load(selectedImageUri)
     .into(imageView);

Glide does all the backend work.

Allan Veloso
  • 5,823
  • 1
  • 38
  • 36
  • While this link may answer the question, link only answers are discouraged on Stack Overflow, you can improve this answer by taking vital parts of the link and putting it into your answer, this makes sure your answer is still an answer if the link gets changed or removed :) – WhatsThePoint Nov 27 '17 at 09:34