0

Hi am getting the following error, when i try to launch my widget, it a note taking app, this is the error I get, please kindly provide a workable answer using my code, would gladly accept the answer that works, thanks in advance:-

05-16 02:53:42.491: E/AndroidRuntime(5335): java.lang.OutOfMemoryError

and the error points to this line in my code:-

 File imgFile = new File(data.get(position).get("path"));
 Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
 imageView.setImageBitmap(myBitmap);
SteD
  • 13,909
  • 12
  • 65
  • 76
  • Possible duplicate of http://stackoverflow.com/questions/12250300/android-image-view-out-of-memory-error and http://stackoverflow.com/questions/11116913/outofmemoryerror-imageview You should resize your image to a proper size. – Logain May 17 '15 at 16:42

2 Answers2

1

You could use Picasso to handle all this for you, which is highly recommended in my opinion. It's as simple as including the library and call this:

Picasso.with(context).load(myBitmap).into(imageView);

Or, you can try the method outlined here in this link.

Specifically, this one.

public static Bitmap decodeSampledBitmapFromResource(Resources res, String resId, int    reqWidth, int reqHeight) {

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

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

 // Decode bitmap with inSampleSize set
 options.inJustDecodeBounds = false;
 options.inPreferredConfig = Config.RGB_565; <---- the options and the one below are important
 options.inDither = true;
 return BitmapFactory.decodeFile(resId, options);
 }
Community
  • 1
  • 1
SteD
  • 13,909
  • 12
  • 65
  • 76
0

This might help someone out there, after several hours without an answer to this. I found a way of handling the outOfMemoryError. I added this line of code in my Manifest file.

android:largeHeap="true"

I added this to the entire application here as below:-

<application
  android:icon="@drawable/ic_launcher"
  android:largeHeap="true"
  android:label="@string/app_name" >

android:largeHeap is the instrument for increasing your allocated memory to app.