0

I want to pass an image from camera or gallery to show it another activity.In another activity i simply typecasted the image view object.but I failed every time. Sender activity here:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==50 && resultCode==RESULT_OK && data != null){
        Bitmap B=(Bitmap) data.getExtras().get("data");
    }
    else if(requestCode==40 && resultCode==RESULT_OK && data != null){
        Bitmap B=(Bitmap) data.getExtras().get("data");
    }
}
karan
  • 8,637
  • 3
  • 41
  • 78
  • https://stackoverflow.com/questions/11519691/passing-image-from-one-activity-another-activity refer this link – Archu Mohan Oct 20 '18 at 05:28
  • 4
    Possible duplicate of [Passing Bitmap between two activities](https://stackoverflow.com/questions/12908048/passing-bitmap-between-two-activities) – Morteza Jalambadani Oct 20 '18 at 05:40
  • In code you provided you just receiving small image preview from camera. Please add your code where you start new activity and code where you try to get image in it. In your sample you not sending anything anywhere. – Andrei Vinogradov Oct 20 '18 at 05:55

1 Answers1

0

Carry bitmap by intent maybe cause app crash if bitmap is too big,so I suggest you save it into storage card and then pass your local path by intent.For example,there is a Bitmap,you saved it into local path /storage/emulated/0/Download/mybitmap.jpg,then you put this path into Intent like intent.putExtra("path","/storage/emulated/0/Download/mybitmap.jpg").In your target Activity get path by getIntent().getStringExtra("path").

you can save bitmap with following method:

public static void saveImage(Bitmap bm, String fileName,boolean needCompress) {  
        try {
            File myCaptureFile = new File(fileName);
            File parent = myCaptureFile.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
            if(needCompress){
                bm.compress(Bitmap.CompressFormat.JPEG, 85, bos);  
            }else{
                bm.compress(Bitmap.CompressFormat.PNG, 100, bos);
            }
            bos.flush();  
            bos.close();  
        } catch (IOException e) {
            throw new RuntimeException("IOException occurred when saving image: " + fileName, e);
        }
    } 
aolphn
  • 2,950
  • 2
  • 21
  • 30