I'm creating my own SlideShow activity where I have a ImageView and I just fill it with the proper image each time the user clicks on next or previous.
These images are big though, and as soon as I navigate through a couple of them I get an OutofMemory probably due to a memory leak of my fault.
The thing is, each time a new image is called, I try to null the previous bitmap so it's garbagecollected, but to no avail.
Am I missing something here?
I am attaching the very simple code of my activity:
public class SlideShowActivity extends Activity {
private static final String LOG_TAG = SlideShowActivity.class.getSimpleName();
private Bitmap currentBitmap;
private Bitmap mDefaultBitmap;
private ImageView imageView;
private ImageButton buttonPrev;
private ImageButton buttonNext;
private int currentImageIndex;
private String imguri[] = { "ruta1/imagen1.jpg", "ruta1/imagen2.jpg", "ruta1/imagen3.jpg", "ruta1/imagen4.jpg", "ruta1/imagen5.jpg", "ruta1/imagen6.jpg" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.slideshow);
mDefaultBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.noimage);
imageView = (ImageView) findViewById(R.id.imageViewCurrentImage);
buttonPrev = (ImageButton) findViewById(R.id.imgbuttonprev);
buttonNext = (ImageButton) findViewById(R.id.imgbuttonnext);
currentImageIndex = 0;
setCurrentImage(currentImageIndex);
buttonPrev.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
currentImageIndex--;
if(currentImageIndex < 0)
currentImageIndex = imguri.length-1;
setCurrentImage(currentImageIndex);
}
});
buttonNext.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
currentImageIndex++;
if(currentImageIndex == imguri.length)
currentImageIndex = 0;
setCurrentImage(currentImageIndex);
}
});
}
private void setCurrentImage(int idx) {
currentBitmap = null;
imageView.setImageBitmap(null);
try {
currentBitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeStream(getAssets().open(imguri[idx])),800, 480, true);
imageView.setImageBitmap(currentBitmap);
} catch (IOException e) {
Log.d(LOG_TAG, "No se pudo abrir el recurso" + imguri[0]);
imageView.setImageBitmap(mDefaultBitmap);
}
}
}
-
your image size is larger than you show.. so first calculate the size of your image then store into creatbitmapscale then use it....go it???QuokMoon– QuokMoon2012年12月28日 17:46:04 +00:00Commented Dec 28, 2012 at 17:46
-
isn't this what I'm doing already?M Rajoy– M Rajoy2012年12月28日 17:57:31 +00:00Commented Dec 28, 2012 at 17:57
3 Answers 3
It's better to call bitmap.recycle() if the bitmap is no longer needed, this can make the system recycle the native memory more actively.
so the setCurrentImage(int idx) can be changed to
private void setCurrentImage(int idx) {
imageView.setImageBitmap(null);
currentBitmap.recycle();
currentBitmap = null;
try {
Bitmap tempBitmap = BitmapFactory.decodeStream(getAssets().open(imguri[idx]));
currentBitmap = Bitmap.createScaledBitmap(tempBitmap, 800, 480, true);
tempBitmap.recycle();
imageView.setImageBitmap(currentBitmap);
} catch (IOException e) {
Log.d(LOG_TAG, "No se pudo abrir el recurso" + imguri[0]);
imageView.setImageBitmap(mDefaultBitmap);
}
}
2 Comments
Use ListView for this purpose .
Use the link : http://www.vogella.com/articles/AndroidListView/article.html
Comments
Well, I don't know what kind of effects can take when you change a lot of times the bitmap of a ImageView this way. But I think you should use the ViewPager that comes with the support library. This way, your implementation will be faster, since it automatically load the next and previous view and remove it when it's no more necessary. And it will be even better for the user, since he/she can slide the image.
To make it even better, you can use this library, that extends the functionality of the ViewPager:
2 Comments
Explore related questions
See similar questions with these tags.