How to send a raster image to a bundle

I am new to android. I want to transfer a bitmap to the bundle. But I cannot find any solution for this. Actually, I'm confused. I want to display an image in a fragment of a dialog. But I do not know how to put in the bundle. Should I send as PutByteArray() ? But if I pass the bitmap as an argument, it points out as an invalid argument.

Here is my code:

 public class MyAlert extends DialogFragment { Bitmap b; public MyAlert newInstance(Bitmap b) { this.b=b; MyAlert frag=new MyAlert(); Bundle args=new Bundle(); args.put("bitByte",b); frag.setArguments(args); return frag; } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { Bitmap bitmap=getArguments().getByteArray("bitByte"); return new AlertDialog().Builder(getActivity()); AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()).setView(R.id.fragid).create(); 
+5
source share
5 answers

First of all, convert it to a Byte array before adding it to the intent, send and decode.

// Convert to a byte array

  ByteArrayOutputStream stream = new ByteArrayOutputStream(); bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); byte[] byteArray = stream.toByteArray(); Bundle b = new Bundle(); b.putByteArray("image",byteArray); // your fragment code fragment.setArguments(b); 

get meaning through intention

 byte[] byteArray = getArgument().getByteArrayExtra("image"); Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length); 
+6
source

No need to convert a bitmap to an array of bytes. You can directly place a raster image in a bundle. Refer to the following code to put the bitmap in the package.

 bundle.putParcelable("BitmapImage",bitmapname); 

Get the bitmap from the bundle using the following code

 Bitmap bitmapimage = getIntent().getExtras().getParcelable("BitmapImage"); 
+11
source

I finally found it #pskink, and ShashiRajan said that we can transfer the bitmap directly. I implemented this way. MyAlert myAlert = new MyAlert(); Bundle b=new Bundle(); b.putParcelable("BitImage",bit); myAlert.setArguments(b); I hope this can be useful for some beginners like me, thanks for helping the guys.

+1
source

If you want to transfer the image using the package, I'm sure that it will help you.

 Bundle bundle = new Bundle(); bundle.putParcelable("bitmap", bitmap); fragment.setArguments(bundle); 
+1
source

I think it’s easier to send the path or address of the image as a string and load it from the other side.

If the image is a web address, you can use the Glide or Picasso libraries and cache them, so it won’t load twice in other actions or fragments.

0
source

Source: https://habr.com/ru/post/1236293/


All Articles