1

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)
Huzain
  • 69
  • 2
  • 8

2 Answers2

3

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

Ognian Gloushkov
  • 2,669
  • 1
  • 20
  • 35
  • but, i want to update that view in another activity, can i do that? – Huzain Nov 08 '16 at 11:13
  • Yes, you should pass the attributes you need and apply them to the view in the other Activity – Ognian Gloushkov Nov 08 '16 at 11:14
  • i have imageview that i should update in another activity, how to do that? – Huzain Nov 08 '16 at 11:19
  • 1
    @Huzain: "i have imageview that i should update in another activity, how to do that?" -- you don't. That is akin to saying "I have a Web page that I should update in another Web page". One Web page does not directly modify the HTML of another Web page. Instead, one Web page updates some shared state (e.g., on the server) that is used by the other Web page. The same thing holds here: you update some state from one activity that the other activity uses when it returns to the foreground. – CommonsWare Nov 08 '16 at 11:51
1

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

Community
  • 1
  • 1
Priyank Patel
  • 12,244
  • 8
  • 65
  • 85