4

im Displaying images from specific folder on sd Card in GridView till know all works fine all images are getting displayed on the gridview but when i change the orientation of the Screen it crashes . below is my code and Crash Log:

    import java.io.File;

import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;

        public class ImageAdapter extends BaseAdapter {
        private Context mContext;
        File root = new File(Environment.getExternalStorageDirectory()
                + File.separator + "MYPICS" + File.separator);
        private File[] fileName = root.listFiles();
        int count=fileName.length;
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
        return fileName.length;
    }

    public Object getItem(int position) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {

            Uri uri = Uri.fromFile(fileName[position]);

        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
            imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
            imageView.setPadding(8, 8, 8, 8);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageURI(uri);
        return imageView;
    }






}

I am getting Memory Out Error ,Below is my Logcat:

 05-16 16:18:31.218: E/AndroidRuntime(26907): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:573)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:439)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.graphics.drawable.Drawable.createFromStream(Drawable.java:657)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.ImageView.resolveUri(ImageView.java:521)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.ImageView.setImageURI(ImageView.java:305)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at com.sachin.imagegrideview.ImageAdapter.getView(ImageAdapter.java:53)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.AbsListView.obtainView(AbsListView.java:1464)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.GridView.onMeasure(GridView.java:935)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3138)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.View.measure(View.java:8313)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.ViewRoot.performTraversals(ViewRoot.java:845)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.os.Looper.loop(Looper.java:130)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at android.app.ActivityThread.main(ActivityThread.java:3687)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at java.lang.reflect.Method.invokeNative(Native Method)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at java.lang.reflect.Method.invoke(Method.java:507)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
05-16 16:18:31.218: E/AndroidRuntime(26907):    at dalvik.system.NativeStart.main(Native Method)
Herry
  • 7,037
  • 7
  • 50
  • 80
Sachin Gurnani
  • 2,444
  • 7
  • 36
  • 45
  • what you have tried before asking question ? search in stackoverflow... – Samir Mangroliya May 16 '12 at 11:06
  • Before this i do it with BitmapFactory but nothing works fine for me – Sachin Gurnani May 16 '12 at 11:10
  • Load Full Image with out any Memory Management you will sure get OOM Error ,Please Refer this site [http://developer.android.com/training/displaying-bitmaps/index.html](Load Bitmaps ) – Herry May 16 '12 at 11:11
  • check this ...https://github.com/nostra13/Android-Universal-Image-Loader – Samir Mangroliya May 16 '12 at 11:16
  • @Herry your comented link is broken – Sachin Gurnani May 16 '12 at 11:17
  • 1
    @SachinGurnani Here is Link http://developer.android.com/training/displaying-bitmaps/index.html – Herry May 16 '12 at 11:19
  • If it's about image size use this code snippet `BitmapFactory.Options bitmapFactoryOptions = new BitmapFactory.Options(); bitmapFactoryOptions.inSampleSize = 2; bitmap = BitmapFactory.decodeFile(imagePath, bitmapFactoryOptions);` – AnujAroshA May 16 '12 at 11:29
  • @saurabhtrivedi File sdImageMainDirectory = new File(path,"image.jpg"); Uri imgUri = Uri.fromFile(sdImageMainDirectory); Intent intent = new Intent( android.provider.MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, imgUri); startActivityForResult(intent, 0); start camera with this intent image is stored in your path specified imgUri path – Sachin Gurnani Oct 15 '12 at 18:59

2 Answers2

4

add this to your manifest for listview activity

android:configChanges="orientation|keyboardHidden"

if this not helps then add this piece of code for your image file to sample the bitmap

private Bitmap decodeFile(File f){
            FileOutputStream os = null;
            try {
                //Decode image size
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;

                FileInputStream fis = new FileInputStream(f);
                BitmapFactory.decodeStream(fis, null, o);
                fis.close();

                int scale = 1;
                if (o.outHeight > IMAGE_MAX_SIZE || o.outWidth > IMAGE_MAX_SIZE) {
                    scale = (int)Math.pow(2, (int) Math.round(Math.log(IMAGE_MAX_SIZE / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
                }

                //Decode with inSampleSize
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize = scale;
                fis = new FileInputStream(f);
                try {

                b = BitmapFactory.decodeStream(fis, null, o2);

                } catch (OutOfMemoryError e) {
                    Log.d("hai","filename"+f);
                    System.gc();
                }
                fis.close();
            } catch (IOException e) {
            }
            return b;
        }

Load lot many images causes the app to run out of memory and force closes.I think this is what happening to your application.the memory issue is a complex issue android while developing an application.this can be solved by manually clearing the unused bitmaps and by using the garbage collector.

Try using System.gc();

Try recycling the bitmap using

Bitmap.recycle();

Make all the unused bitmap null.

Deallocate all the unused memory.

This all will help you a lot and also go through this link.Use memory analyzer it will help you spot out the Deallocated memory>try this link

Community
  • 1
  • 1
Jackson Chengalai
  • 3,907
  • 1
  • 24
  • 39
  • im facing same as i mention above – Sachin Gurnani May 16 '12 at 11:27
  • is there any issue with image size – Sachin Gurnani May 16 '12 at 11:28
  • if the image size is bigg.then there will be a chance for oom exception.so scale down the bitmap is a simple way.check my answer – Jackson Chengalai May 16 '12 at 11:34
  • File[] files = root.listFiles();for (int i = 0; i < fileNames.length; i++) { Uri uri = Uri.fromFile(files[i]); String path="/mnt/sdcard/MYPICS/MyPic"+i+".png"; /*BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = 8; Bitmap yourSelectedImage = BitmapFactory.decodeFile(path);*/ Bitmap bm=decodeFile(files[i]); imageView.setImageBitmap(bm); } – Sachin Gurnani May 16 '12 at 11:38
  • but it display same image 4 time and i have 6 images in my folder – Sachin Gurnani May 16 '12 at 11:46
  • now ur imageview replacing each other.create a imageview array and save each image to that imageview[i] and in your getview add set your imageview array like imageview[position] – Jackson Chengalai May 16 '12 at 11:53
1

<activity android:configChanges="keyboardHidden|orientation"></activity>

in your manifest.xml edit your activity tag and try

Dhruvil Patel
  • 2,910
  • 2
  • 22
  • 39