Doha salem
Doha salem

Reputation: 1

how to pass the bitmap from one activity to another?

I have two activity , the first activity (camera Activity) contain two button one to open the camera , and the second activity display form contain the image view I need to pass the bitmap that capture from camera in another FORM activity ,the camera open and taken the image and this image put on imageview on another Activity but my code is taken image and return to first activity does not display on second activity ?

the camera code

public class camera extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int CAMERA_REQUEST = 1888;
ImageView imageView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);


    //register
   Button camera = (Button)findViewById(R.id.s_camera);
    camera.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            open();
        }
    });

    Button call = (Button)findViewById(R.id.s_call);
    call.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Uri uri = Uri.parse("tel:0555064740");
            Intent intent = new Intent(Intent.ACTION_VIEW,uri);
            startActivity(intent);
        }
    });



}

public void open(){


   Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent,0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==CAMERA_REQUEST&&resultCode==RESULT_OK) {
        Bitmap bp = (Bitmap) data.getExtras().get("data");
        Intent intent = new Intent(getApplicationContext(), formActivity.class);
        intent.putExtra("BitmapImage", bp);

    }


    }

the formActivity

public class formActivity  extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
Bitmap photo;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_form);
    getData();



}
private void getData(){

    Bitmap bitImage=getIntent().getParcelableExtra("BitmapImage");
    imageView.setImageBitmap(bitImage);

}

any one can help me ???

Upvotes: 0

Views: 7458

Answers (3)

Sohel Mahmud
Sohel Mahmud

Reputation: 185

Since the question is quite old, here is a solution that works better without any compression or quality loss and also don't have to create temporary bitmap file

  1. Create a BitmapHelper class

    public class BitmapHelper {
    private Bitmap bitmap = null;
    private static final BitmapHelper instance = new BitmapHelper();
    
    public BitmapHelper(){
    
    }
    
    public static BitmapHelper getInstance(){
        return instance;
    }
    
    public Bitmap getBitmap(){
        return bitmap;
    }
    
    public void setBitmap(Bitmap bitmap){
        this.bitmap = bitmap;
    }
    

    }

  2. Now get or set your bitmap from any activity like this

    //to set the bitmap
    BitmapHelper.getInstance().setBitmap(saveBitmap);
    //to get the bitmap
    anyImageView.setImageBitmap(BitmapHelper.getInstance().getBitmap());
    

Upvotes: 0

Spring Breaker
Spring Breaker

Reputation: 8251

Bitmap implements Parcelable, so you could always pass it in the intent:

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

and retrieve it on the other end:

Intent intent = getIntent();
Bitmap bitmap = (Bitmap) intent.getParcelableExtra("BitmapImage");

you miss startActivity(intent) in the first activity as commented by blackbelt. Update it in your onActivityResult().

reference https://stackoverflow.com/a/2459624/1665507

You can find a working example in below link,

http://androidmaterial.blogspot.in/2012/07/how-to-pass-bitmap-between-activities.html

EDIT:

As commented by others, sending big images as Parcelable is not recommended. You can use the below method also,

Pass Bitamp as Extended Data

    ByteArrayOutputStream bStream = new ByteArrayOutputStream();
 //Compress it before sending it to minimize the size and quality of bitmap.
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, bStream);
        byte[] byteArray = bStream.toByteArray();
    
        Intent anotherIntent = new Intent(this, anotherActivity.class);
        anotherIntent.putExtra("image", byteArray);
        startActivity(anotherIntent);
        finish();

Retrieve Bitmap in Other Activity

 if(getIntent().hasExtra("image")) 
   {
    ImageView previewThumbnail = new ImageView(this);
    Bitmap b = BitmapFactory.decodeByteArray( getIntent().getByteArrayExtra("image"),0,getIntent().getByteArrayExtra("byteArray").length);        
    previewThumbnail.setImageBitmap(b);
    }

reference https://stackoverflow.com/a/12908133/1665507

Also there is a other way. Though you are taking picture from Camera or Gallery, you can always get the exact URI path of the image and can pass that path to the other activity. Sending big images requires a lot of memory so as per your need, choose the best way.

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157437

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==CAMERA_REQUEST&&resultCode==RESULT_OK) {
        Bitmap bp = (Bitmap) data.getExtras().get("data");
        Intent intent = new Intent(getApplicationContext(), formActivity.class);
        intent.putExtra("BitmapImage", bp);

    }
 }

in the snippet you miss startActivity(intent). On the other hand, in formActivity.class, before imageView.setImageBitmap(bitImage); you have to initialize imageView otherwise your app will crash. As mentioned in the comment box, it would be better to provide the URI of the picture instead of passing the whole bitmap to the formActivity

Upvotes: 1

Related Questions