blurbs78 blurbs78 - 1 year ago 156
Android Question

Android Studio OutOfMemoryError when trying to get image through Picasso

EDIT: So apparently the images I'm trying to use are too large. Anyway I can get around this? I'm trying to eventually crop them and set them as my wallpaper programmatically. I've tried a smaller image and it works. Any tips for larger images?

So I'm trying to get an image from a url using Picasso, but when I use the .get() method I'm getting this:

java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(
at java.util.concurrent.FutureTask.finishCompletion(
at java.util.concurrent.FutureTask.setException(
at android.os.AsyncTask$SerialExecutor$
at java.util.concurrent.ThreadPoolExecutor.runWorker(
at java.util.concurrent.ThreadPoolExecutor$
Caused by: java.lang.OutOfMemoryError
at Method)
at com.squareup.picasso.BitmapHunter.decodeStream(
at com.squareup.picasso.BitmapHunter.hunt(
at com.squareup.picasso.RequestCreator.get(

I'm executing the Picasso code in AsyncTask because that's where I have some network code fetching the url I need. However, even if I comment out everything and give the url to Picasso directly, the problem persists. With everything commented out my code looks like this:

protected Bitmap doInBackground(String... name) {
try {
return Picasso.with(myContext)

} catch (Exception e) {
android.util.Log.e(TAG, "Failed to retrieve submissions", e);
return null;

I've tried everything I could think of including increasing the heap size, adding the largeHeap tag to the manifest, etc. Is this a problem with my code or Android Studio? When using the Memory Monitor while my app is running it says "Free[0.35 MB] Allocated[15.02 MB]" before I even do anything. That can't be right can it? I'm still new to all this so if I am misunderstanding something please let me know. Thanks.

Answer Source

Use resize with Picasso

return Picasso.with(myContext)
                .resize(500, 300)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download