Abhs Make
Abhs Make

Reputation: 11

How to resize an image array?

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView iv_images;

    final LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.imageadapter1, null);

    iv_images = (ImageView) convertView.findViewById(R.id.iv_images1);

    iv_images.setImageResource(images[position]);

    return convertView;
}

java.lang.OutOfMemoryError: Failed to allocate a 9042060 byte allocation with 8030424 free bytes and 7MB until OOM

Upvotes: 0

Views: 102

Answers (2)

Oleg Golomoz
Oleg Golomoz

Reputation: 532

You can use libraries such as Picasso

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ImageView iv_images;

    final LayoutInflater inflater = (LayoutInflater) 
    mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.imageadapter1, null);

    iv_images = (ImageView) convertView.findViewById(R.id.iv_images1);

    Picasso.get()
       .load(images[position])
       .resize(YOUR_WIDTH, YOUR_HEIGHT)
       .centerCrop()
       .into(iv_images)

    return convertView;
}

Don't forgot to add dependecies.

Gradle:

implementation 'com.squareup.picasso:picasso:2.71828'

Maven:

<dependency>
  <groupId>com.squareup.picasso</groupId>
  <artifactId>picasso</artifactId>
  <version>2.71828</version>
</dependency>

Upvotes: 0

Hitesh Tarbundiya
Hitesh Tarbundiya

Reputation: 464

You could just use a smaller image.

  1. Google have actually published a guide on avoiding OutOfMemoryErrors here which will help a lot, though I had to use a smaller image size as well.
  2. One method that will almost definitely work is to set android:largeHeap="true" in your manifest, between your application tags. This will increase your heap size, but may make your app lag a little.
  3. Make use of WebP image for loading if you had large number of image.

You may, try this link https://developer.android.com/topic/performance/graphics/load-bitmap

Upvotes: 1

Related Questions