problem in using bitmap in android
I have the following issue:
First activity
The android camera is opened and the user can take a picture.Once the picture is taken the second activity is launched.
Second activity
In this activity the user has the posibility to view the photo. For that I receive from the first activity the picture, like this:
Bundle extras = getIntent().getExtras();
and I create the following bitmap:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 5;
options.inDither = true;
byte[] imageData = extras.getByteArray("imageData");
myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);
Matrix mat = new Matrix();
mat.postRotate(90);
bitmapResult = Bitmap.createBitmap(myImage, 0, 0, myImage.getWidth(),
myImage.getHeight(), mat, true);
//...operations on the bitmapResult
The result bitmap is the stored into the sdcard
and before leaving this activity I recycle my bitmap in order to avoid memory i开发者_开发百科ssue:
bitmapResult.recycle();
bitmapResult=null;
After all this is done we move on to activity 3.
Third activity
In this activity the user can see the picture in different size.The picture is taken from the sdcard
.
The big problem is when the user hits the back button is taken from the activity three
to activity two
, but my app crashes because is trying to do operations on bitmap that has been recycled.
And if I don't recycle the bitmap in the second activity I get OutOfMemeory Exception
when I go a few times up and down through my application.
Any ideas?
When your user is in Activity 2 don't go directly to Activity 3 (the gallery). First, finish Activity 2 and return a result to Activity 1. Then start Activity 3.
Your first/main activity:
public class TakeAPicture extends Activity {
public static final int REQUEST_PICTURE_WAS_TAKEN = 100;
public static final int REQUEST_SHOW_GALLERY = 101;
public static final int RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY = 102;
public static final int RESULT_BACK_FROM_GALLERY = 103;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.takeapicture);
Button btn = (Button) findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(),
PictureTaken.class);
startActivityForResult(i, REQUEST_PICTURE_WAS_TAKEN);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_PICTURE_WAS_TAKEN:
if (resultCode == RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY) {
Intent i = new Intent(getApplicationContext(), Gallery.class);
startActivityForResult(i, REQUEST_SHOW_GALLERY);
}
break;
default:
break;
}
}
}
And in your second activity you have to do something like this:
public class PictureTaken extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.picturetaken);
Button btn = (Button) findViewById(R.id.button2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PictureTaken.this
.setResult(TakeAPicture.RESULT_PICTURE_WAS_TAKEN_SHOW_GALLERY);
PictureTaken.this.finish();
}
});
}
}
Regards
精彩评论