Problem using bitmap in android

I have the following problem:

First activity 

The android camera opens and the user can take a picture. After the picture is taken, the second action starts.

 Second activity 

In this exercise, the user has the opportunity to view the photo. To do this, I get an image from the first action, for example:

 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 bitmap of the result is stored in sdcard and before leaving this activity, I process the bitmap to avoid the memory problem:

 bitmapResult.recycle(); bitmapResult=null; 

After that, we go to step 3.

 Third activity 

In this exercise, the user can see an image of different sizes. Image taken from sdcard .

The big problem is when the user clicks the back button, it is taken from activity three to action two , but my application crashes because it is trying to do operations on bitmap that has been recycled.

And if I donโ€™t recycle the bitmap in the second action, I get an OutOfMemeory Exception when I go up and down my application several times.

Any ideas?

+4
source share
1 answer

When your user is in Activity 2 , do not go directly to Activity 3 (gallery). First complete Activity 2 and return the result to Activity 1 . Then run Action 3 .

Your first / main action:

 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 the second exercise, you should 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(); } }); } } 

Hello

0
source

All Articles