Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send the bitmap into bundle

Tags:

android

bitmap

I'm new to android. I want to pass bitmap into Bundle. But I can't find any solution for it. Actually, I'm confused. I want to display an image in a Dialog fragment. But I don't know how to put into Bundle. Should I send as PutByteArray()? But if I pass bitmap as an argument, it is stating as a wrong 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();
like image 543
Sayyaf Avatar asked Nov 19 '15 06:11

Sayyaf


2 Answers

No need to convert bitmap to byte array. You can directly put bitmap into bundle. Refer following code to put bitmap into bundle.

bundle.putParcelable("BitmapImage",bitmapname);

Get bitmap from Bundle by following code

Bitmap bitmapimage = getIntent().getExtras().getParcelable("BitmapImage");
like image 134
Shashi Ranjan Avatar answered Oct 03 '22 08:10

Shashi Ranjan


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

//Convertion to 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 Value via intent

byte[] byteArray = getArgument().getByteArrayExtra("image");
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
like image 28
EminenT Avatar answered Oct 03 '22 10:10

EminenT