1

I'm sorry about probably posting a question similar to many others but I cannot find a solution to this or find a similar situation to where this error occurs. I have googled and googled all week trying to find an answer to this but not luck as of yet. I'm fairly new to android so if it is obvious, this is probably why!

These are the steps leading to the error:

The app uses the camera to take a picture
This image is then stored in a folder on the SDCard
A copy of this file is made and compressed down to 50-100kb per image (this is what I'm trying to display)
I then store the initial size and file name of the image in a sqlite database
when the activity (onResume I do this) is loaded If there are any file names in the database, it gets the file name and displays the image - this is where the error is.

if(c.getCount() > 0) {

   String[] cols = new String[] {DBAdapter.FILENAME,DBAdapter.DATETIME};

    int[] values = new int[] {R.id.thumbnail,R.id.datetime_stamp};

    SimpleCursorAdapter cursorAdapter = new SimpleCursorAdapter(this, R.layout.image_listview, c, cols, values, 0);

    ListView listView = (ListView) findViewById(android.R.id.list);
    listView.setAdapter(cursorAdapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> listView, View view, int position, long id) {
            String row = c.getString(c.getColumnIndexOrThrow(DBAdapter.KEY_ID)); // row ID from transactions
            Toast.makeText(getBaseContext(), row, Toast.LENGTH_SHORT).show();
        }
    });
}

I am obviously missing something but its just driving me crazy!

The errors I'm getting if any of this helps are:

06-13 22:17:25.269: E/AndroidRuntime(24153): FATAL EXCEPTION: main
06-13 22:17:25.269: E/AndroidRuntime(24153): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:511)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:325)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:350)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.graphics.drawable.Drawable.createFromPath(Drawable.java:831)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ImageView.resolveUri(ImageView.java:573)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ImageView.setImageURI(ImageView.java:350)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.support.v4.widget.SimpleCursorAdapter.setViewImage(SimpleCursorAdapter.java:197)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.support.v4.widget.SimpleCursorAdapter.bindView(SimpleCursorAdapter.java:143)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.AbsListView.obtainView(AbsListView.java:1451)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.makeAndAddView(ListView.java:1786)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.fillDown(ListView.java:705)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.fillGap(ListView.java:676)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:3579)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:2294)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.widget.ListView.onTouchEvent(ListView.java:3493)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.View.dispatchTouchEvent(View.java:3935)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:934)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:973)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1741)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1172)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.app.Activity.dispatchTouchEvent(Activity.java:2135)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1725)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewRoot.deliverPointerEvent(ViewRoot.java:2241)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.view.ViewRoot.handleMessage(ViewRoot.java:1925)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.os.Handler.dispatchMessage(Handler.java:130)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.os.Looper.loop(SourceFile:351)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at android.app.ActivityThread.main(ActivityThread.java:3814)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at java.lang.reflect.Method.invokeNative(Native Method)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at java.lang.reflect.Method.invoke(Method.java:538)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:659)
06-13 22:17:25.269: E/AndroidRuntime(24153):    at dalvik.system.NativeStart.main(Native Method)

Thanks in advance for any help.

Derek
  • 952
  • 3
  • 13
  • 34
TheT4llOne
  • 43
  • 5
  • 1
    I suspect this is in fact just like the many similar questions on here. You're trying to load a very large bitmap. Doesn't matter if it compresses to 50-100KB on disk, in memory it takes up more. – kabuko Jun 13 '13 at 21:29
  • @TheT4llOne - have you looked at the answers to [this question](http://stackoverflow.com/questions/3956702/java-lang-outofmemoryerror-bitmap-size-exceeds-vm-budget)? – Derek Jun 13 '13 at 21:54
  • @kabuko - I don't see what else it could be as this is all the loading bit of the activity, remove this and the activity does not crash. Here is a copy of the entire activity code if it helps: [link](http://pastebin.com/PN5H3QyQ) – TheT4llOne Jun 17 '13 at 14:47
  • @Derek - Yes, I have seen this question and it did not help much, but thank you for suggesting it :) – TheT4llOne Jun 17 '13 at 14:49

1 Answers1

0

First of all: dont save pictures in your database. Save it to the sd and put a reference in the DB (filepath). Database are not made for big binary data (could slow them down drastically).

Secondly, I dont think the bug is in the quote snippet you provided. If you take a picture from the camera, I assume it is at least 5MP, you have to take care not to load the whole picture as a Bitmap, as it would need at least 114 MB Ram for a 5MP picture @24bit, but you only have about 24. What you want to do is load a scaled down version into your UI. You first get the dimension with:

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

Then you check how much you have to scale it down (should be a power of 2) and decode it with the correct sampling option

 // Calculate inSampleSize
options.inSampleSize = 8;

// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);

Read more at http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Patrick
  • 33,984
  • 10
  • 106
  • 126
  • Sorry if I was unclear, I currently already have the path in the database and it's when I load the activity and there is more than 1 image present it crashes. How do I load the scaled image from a path and insert this into the listview? Thanks – TheT4llOne Jun 14 '13 at 09:01
  • Check the link I provided, its a step by step how to scale down and load the bitmap: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html I think the rest is straigt forward. – Patrick Jun 14 '13 at 09:22
  • Hi @for3st, thanks for the help so far and sorry to sound like a broken record but I still cannot figure this out as I only have the path and it is going directly to the listView, here is the full code so you can see: [link](http://pastebin.com/PN5H3QyQ) – TheT4llOne Jun 14 '13 at 11:00