0

want i want to do is take a picture with my camera and than pass that image to another activity, in the next activity i would like to get that image and upload it. i tried using the following code and it works but the quality is very low.

private void putBitmapToIntentAndStartActivity(Bitmap bitmap)
{
    Intent i = new Intent(getActivity(), ImagePreviewActivity.class);
   // Bitmap b=scaleDown(bitmap,400,true); 
    ByteArrayOutputStream bs = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 100, bs);

    i.putExtra("imageByteArray", bs.toByteArray());
    startActivity(i);


}

if i remove the marked line with the scale downmethod it wont work. if i use it it works but the quality is very low. is there any ideas how to pass that image and get it in the next activity with its full quality?

thanks! im sure its a very common issue.

aviv_elk
  • 416
  • 1
  • 9
  • 21
  • 2
    If your image comes from a file (and it does if you instruct the camera like that) you can much better pass the path to that file to the next activity. – greenapps Apr 20 '15 at 11:49
  • 1
    possible duplicate of [How can I pass a Bitmap object from one activity to another](http://stackoverflow.com/questions/2459524/how-can-i-pass-a-bitmap-object-from-one-activity-to-another) – Piotr Golinski Apr 20 '15 at 11:50

1 Answers1

0

If you want to tranfer bitmap between Activites/Fragments


Activity

To pass a bitmap between Activites

Intent intent = new Intent(this, Activity.class);
intent.putExtra("bitmap", bitmap);

And in the Activity class

Bitmap bitmap = getIntent().getParcelableExtra("bitmap");

Fragment

To pass a bitmap between Fragments

SecondFragment fragment = new SecondFragment();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
fragment.setArguments(bundle);

To receive inside the SecondFragment

Bitmap bitmap = getArguments().getParcelable("bitmap");

Transfering Large Bitmaps (Compressing)

If you are getting failed binder transaction, this means you are exceeding the binder transaction buffer by transferring large element from one activity to another activity.

So in that case you have to compress the bitmap as an byte's array and then uncompress it in another activity, like this

In the FirstActivity

Intent intent = new Intent(this, SecondActivity.class);

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPG, 100, stream);
byte[] bytes = stream.toByteArray(); 
intent.putExtra("bitmapbytes",bytes);

And in the SecondActivity

byte[] bytes = getIntent().getByteArrayExtra("bitmapbytes");
Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
capt.swag
  • 10,335
  • 2
  • 41
  • 41