Skip to content Skip to sidebar Skip to footer

OutOfMemoryError In My GridAdapter

I'm trying create adapter for my gridView which should contains photo, which I chose in gallery. But I have following OutOfMemoryError in line 'return row' public class GridViewAd

Solution 1:

Image loading is a way way more complex thema than this, if you're interested on it, there's lots of topics/blog/etc you can read online. But StackOverflow is a direct question-answer, so,

To fix the issue, import this library: https://github.com/square/picasso

and then replace those lines:

File f = new File(images_urls.get(position));  
Uri imageUri = Uri.fromFile(f);
holder.image.setImageURI(imageUri);

with:

Picasso
  .with(holder.image.getContext())
  .load(image_urls.get(position))
  .into(holder.image);

this library will take care of proper managing the memory. Make sure to further check the library API as you can put extra commands to resize the image on-screen so it uses less memory.


Solution 2:

In Your AndroidManifest.xml

add android:largeHeap="true" to your < application >

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="18" />

<application
    android:largeHeap="true"
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.sample.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And to avoid OutofMemory Error you should use Universal Image Loader Link or Picasso Library Link.


Post a Comment for "OutOfMemoryError In My GridAdapter"