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

前端 未结 10 900
隐瞒了意图╮
隐瞒了意图╮ 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 04:01

    Actually, passing a bitmap as a Parcelable will result in a "JAVA BINDER FAILURE" error. Try passing the bitmap as a byte array and building it for display in the next activity.

    I shared my solution here:
    how do you pass images (bitmaps) between android activities using bundles?

    0 讨论(0)
  • 2020-11-22 04:02

    If the image is too large and you can't save&load it to the storage, you should consider just using a global static reference to the bitmap (inside the receiving activity), which will be reset to null on onDestory, only if "isChangingConfigurations" returns true.

    0 讨论(0)
  • 2020-11-22 04:04

    It might be late but can help. On the first fragment or activity do declare a class...for example

       @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            description des = new description();
    
            if (requestCode == PICK_IMAGE_REQUEST && data != null && data.getData() != null) {
                filePath = data.getData();
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), filePath);
                    imageView.setImageBitmap(bitmap);
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    constan.photoMap = bitmap;
                } catch (IOException e) {
                    e.printStackTrace();
                }
           }
        }
    
    public static class constan {
        public static Bitmap photoMap = null;
        public static String namePass = null;
    }
    

    Then on the second class/fragment do this..

    Bitmap bm = postFragment.constan.photoMap;
    final String itemName = postFragment.constan.namePass;
    

    Hope it helps.

    0 讨论(0)
  • 2020-11-22 04:06

    Passsing bitmap as parceable in bundle between activity is not a good idea because of size limitation of Parceable(1mb). You can store the bitmap in a file in internal storage and retrieve the stored bitmap in several activities. Here's some sample code.

    To store bitmap in a file myImage in internal storage:

    public String createImageFromBitmap(Bitmap bitmap) {
        String fileName = "myImage";//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;
    }
    

    Then in the next activity you can decode this file myImage to a bitmap using following code:

    //here context can be anything like getActivity() for fragment, this or MainActivity.this
    Bitmap bitmap = BitmapFactory.decodeStream(context.openFileInput("myImage"));
    

    Note A lot of checking for null and scaling bitmap's is ommited.

    0 讨论(0)
提交回复
热议问题