Android: Showing photos runs out of memory

Posted by Tom Beech on Stack Overflow See other posts from Stack Overflow or by Tom Beech
Published on 2012-10-24T10:35:07Z Indexed on 2012/10/24 11:01 UTC
Read the original article Hit count: 255

Filed under:
|
|

I'm using a dialog box to display images in my android project. The first one opens fine, but when I close it and do the process again to show a different one the app falls over with a memory error (it's running on a samsung galaxy s3 - so shouldnt be an issue).

Error:

10-24 11:25:45.575: E/dalvikvm-heap(29194): Out of memory on a 31961104-byte allocation.
10-24 11:25:45.580: E/AndroidRuntime(29194): FATAL EXCEPTION: main
10-24 11:25:45.580: E/AndroidRuntime(29194): java.lang.OutOfMemoryError
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:587)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:389)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:418)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.graphics.drawable.Drawable.createFromPath(Drawable.java:882)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.widget.ImageView.resolveUri(ImageView.java:569)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.widget.ImageView.setImageURI(ImageView.java:340)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at com.directenquiries.assessment.tool.AddAsset.loadPhoto(AddAsset.java:771)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at com.directenquiries.assessment.tool.AddAsset$11.onClick(AddAsset.java:748)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:936)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.widget.AdapterView.performItemClick(AdapterView.java:292)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.widget.AbsListView.performItemClick(AbsListView.java:1359)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2988)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.widget.AbsListView$1.run(AbsListView.java:3783)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.os.Handler.handleCallback(Handler.java:605)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.os.Looper.loop(Looper.java:137)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at android.app.ActivityThread.main(ActivityThread.java:4517)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
10-24 11:25:45.580: E/AndroidRuntime(29194):    at dalvik.system.NativeStart.main(Native Method)

Loading code:

public void loadPhotoList(){

    Cursor f = db.rawQuery("select * from stationphotos where StationObjectID =  '"+ checkStationObjectID + "'", null);  
    final ArrayList<String> mHelperNames= new ArrayList<String>();

            if(f.getCount() != 0) {
              f.moveToFirst();

                f.moveToFirst();
                while(!f.isAfterLast()) {
                    mHelperNames.add(f.getString(f.getColumnIndex("FilePath")));
                    f.moveToNext();
                }
            }
     f.close();
     final String [] nameStrings = new String [mHelperNames.size()];

     for(int i=0; i<mHelperNames.size(); i++)
        nameStrings[i] = mHelperNames.get(i).toString();


    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    builder.setTitle("Select Picture");
    builder.setItems(nameStrings, new DialogInterface.OnClickListener() {

       public void onClick(DialogInterface dialog, int item) {

          loadPhoto(mHelperNames.get(item).toString());

       }

    });

    AlertDialog alert = builder.create();

    alert.show();

 }


public void loadPhoto(String imagepath){

    Dialog dialog = new Dialog(this);
    dialog.setContentView(R.layout.activity_show_image);
    dialog.setTitle("Image");
    dialog.setCancelable(true);

    ImageView img = (ImageView) dialog.findViewById(R.id.imageView1);
    img.setImageResource(R.drawable.ico_partial);
    Uri imgUri = Uri.parse(imagepath);
    img.setImageURI(imgUri);


    dialog.show();
}

© Stack Overflow or respective owner

Related posts about java

Related posts about android