Huzain
Huzain

Reputation: 69

Passing View with Intent

I want to passing my viewto update my view in other activity. This is my code to passing view.

emp_photo_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                i.putExtra("container", (Serializable) viewDialog);
                ((EmployeeActivity)context).startActivityForResult(i, 2017);
            }
        });

Then i want to update my view in other activity

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 2017 && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();
        View apa = (View) data.getSerializableExtra("content");
        //View dialog = View.inflate(getApplicationContext(),R.layout.dialog_employee_edit,null);
        ImageView imageView = (ImageView) apa.findViewById(R.id.emp_photo_edit);
        imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
    }
}

But it show Exception.

  FATAL EXCEPTION: main 
  java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to java.io.Serializable
    at com.fingerspot.hz07.revocloud.adapter.EmployeeAdapter$MyViewHolder$5.onClick(EmployeeAdapter.java:334)
    at android.view.View.performClick(View.java:4084)
    at android.view.View$PerformClick.run(View.java:16966)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 1

Views: 6496

Answers (2)

Priyank Patel
Priyank Patel

Reputation: 12382

You can not pass a view (imageview) in intent.

You should pass image bitmap as ByteArray or Parcelable in intent like below.

Pass bitmap as ByteArray:

First Convert image into ByteArray and then pass into Intent and in next activity get ByteArray from Bundle and convert into image(Bitmap) and set into ImageView.

Convert Bitmap to ByteArray and pass into Intent:-

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

Intent intent = new Intent(this, NextActivity.class);
intent.putExtra("picture", byteArray);
startActivity(intent);

Get ByteArray from Bundle and convert into Bitmap image:-

Bundle extras = getIntent().getExtras();
byte[] byteArray = extras.getByteArray("picture");

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);

image.setImageBitmap(bmp);

OR

Pass bitmap as Parcelable:

Pass Bitmap directly into Intent as Parcelable extra and get bitmap as Parcelable extra in next activity from Bundle, but the problem is if your Bitmap/Image size is big at that time the image is not load in next activity.

Checkout get ImageView's image and send it to an activity with Intent

Upvotes: 1

Ognian Gloushkov
Ognian Gloushkov

Reputation: 2659

Arguments passed to the bundle should implement the Serializable or Parcelable interface. The LinearLayout doesn't. The best solution is to pass the data inside the view to the Intent and apply that to a view in the receiving Activity

Upvotes: 3

Related Questions