How can I pass a Bitmap object from one activity to another

前端 未结 10 940
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 03:39

In my activity, I create a Bitmap object and then I need to launch another Activity, How can I pass this Bitmap object from the sub-ac

10条回答
  •  死守一世寂寞
    2020-11-22 03:59

    All of the above solutions doesn't work for me, Sending bitmap as parceableByteArray also generates error android.os.TransactionTooLargeException: data parcel size.

    Solution

    1. Saved the bitmap in internal storage as:
    public String saveBitmap(Bitmap bitmap) {
            String fileName = "ImageName";//no .png or .jpg needed
            try {
                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
                FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
                fo.write(bytes.toByteArray());
                // remember close file output
                fo.close();
            } catch (Exception e) {
                e.printStackTrace();
                fileName = null;
            }
            return fileName;
        }
    
    1. and send in putExtra(String) as
    Intent intent = new Intent(ActivitySketcher.this,ActivityEditor.class);
    intent.putExtra("KEY", saveBitmap(bmp));
    startActivity(intent);
    
    1. and Receive it in other activity as:
    if(getIntent() != null){
      try {
               src = BitmapFactory.decodeStream(openFileInput("myImage"));
           } catch (FileNotFoundException e) {
                e.printStackTrace();
          }
    
     }
    
    
    

提交回复
热议问题