问题
I have ImageView & one button on my ImageActivity. when the button is clicked the available images are listed, when one of them is clicked, it is loaded into the ImageView.
imageView xml:
<ImageView
android:id="@+id/imageView"
android:layout_width="300dip"
android:layout_height="300dip"
android:src="@android:drawable/alert_light_frame"
/>
Please see the code:
public class ImageActivity extends Activity {
private static final int SELECT_PICTURE = 1;
private String selectedImagePath;
private ImageView imageView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.comments_detail);
imageView = (ImageView) findViewById(R.id.imageView);
((Button) findViewById(R.id.BtnBrowse))
.setOnClickListener(new OnClickListener()
{
public void onClick(View arg0)
{
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}
});
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
//System.out.println("Image Path : " + selectedImagePath);
imageView.setImageURI(selectedImageUri);
}
}
}
public String getPath(Uri uri)
{
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
}
Issue: The issue is that when i click the button the available images are displayed, i selects one of them & it is loaded in the ImageView, when i repeat these steps few time the application throws throws the exception: java.lang.OutOfMemoryError: bitmap size exceeds VM budget
I goggled it & found that this issue is because of memory leaks but i am unable to find whats wrong with my code. Can anyone spend some of his valuable time to help me?
Thanks.
回答1:
If you do a series of setImageURIs
, you are loading a series of bitmaps into the view. If you are getting the java.lang.OutOfMemoryError: bitmap size exceeds VM budget exception
, that implies that the imageView
does not recycle the previous bitmap when you load a new one. So you
- either need to encourage the imageView to recycle the previous bitmap - possibly with
setImageView("")
- or get the bitmap from the URI and use
setImageBitmap(android.graphics.Bitmap)
instead; then you can dosetImageBitmap(null)
andbitmap.recycle()
.
回答2:
Well calling the system.gc() worked!! Before the image is assigned to the imageView, i called the System.gc(), to take care of memory & it works.
Look at the modified code:
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (resultCode == RESULT_OK)
{
if (requestCode == SELECT_PICTURE)
{
// calling GC to take care of memory, if you remove this one line of code
// application will throw the error "VM out of memory error"
System.gc();
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
imageView.setImageURI(selectedImageUri);
}
}
}
Thanks @Torid.
Can someone comment on it? is this correct way to call the GC in this situation?
来源:https://stackoverflow.com/questions/8000219/java-outofmemoryerror-bitmap-size-exceeds-vm-budget