Janusz j.
Janusz j.

Reputation: 110

ImageView.SetImageResource method takes a lot of memory (40%)

I got app with SQLite database with saved names of drawables (strings). Multiple drawables are saved in res/drawable directory with their scalling sizes drawable-hdpi, drawable-mdpi, drawable-xhdpi etc. This device use mdpi. Their size is approx since 80 kB. Each time I call update() method I call SetImageResource method for the same ImageViewand I only change image fileand it takes approx 0,8 MB of memory increase. How to avoid this?

private ImageView mImageName;

private void update(){
mImageName.setImageResource(getResources().getIdentifier("@drawable/" + mQuestionLibrary.getImage(mQuestionNumber), null, getPackageName()));
}

Upvotes: 1

Views: 187

Answers (2)

Janusz j.
Janusz j.

Reputation: 110

I found the solution. The problem was that I have had setted in ImageView layout properties: adjustViewBounds and unneccesarry src. Those properties takes a lot of memory.

<ImageView
                android:id="@+id/iV_pytanie"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@drawable/tab1"
                />

I deleted them and memory decreased by approx 0,5 MB for each image.

Upvotes: 1

Khalid Taha
Khalid Taha

Reputation: 3313

You have to use Image Resource Importer to have multiple sizes for different screens.

Then you need to set your Image by this code:

mImageName.setImageResource(R.drawable.resource_name);

enter image description here

you can also moodify the mQuestionLibrary.getImage(mQuestionNumber) to return the resource id instead of resource name, and then use:

mImageName.setImageResource(mQuestionLibrary.getImage(mQuestionNumber));

Upvotes: 1

Related Questions