How to send image from one activity to another in android?

前端 未结 8 1886
忘掉有多难
忘掉有多难 2020-12-10 08:18

I am having a imageView in one class and on clicking the imageView a dialog box appear which has two option to take a image from camera or open the image gallery of device.

8条回答
  •  佛祖请我去吃肉
    2020-12-10 08:46

    I had to rescale the bitmap a bit to not exceed the 1mb limit of the transaction binder. You can adapt the 400 the your screen or make it dinamic it's just meant to be an example. It works fine and the quality is nice. Its also a lot faster then saving the image and loading it after but you have the size limitation.

    public void loadNextActivity(){
    Intent confirmBMP = new Intent(this,ConfirmBMPActivity.class);
    
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    Bitmap bmp = returnScaledBMP();
    bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    
    confirmBMP.putExtra("Bitmap",bmp);
    startActivity(confirmBMP);
    finish();
    
    }
    public Bitmap returnScaledBMP(){
    Bitmap bmp=null;
    bmp = tempBitmap;
    bmp = createScaledBitmapKeepingAspectRatio(bmp,400);
    return bmp;
    }
    

    After you recover the bmp in your nextActivity with the following code:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_confirmBMP);
    Intent intent = getIntent();
    Bitmap bitmap = (Bitmap) intent.getParcelableExtra("Bitmap");
    
    }
    

    I hope my answer was somehow helpfull. Greetings

提交回复
热议问题