用意图传递视图

时间:2016-11-08 11:02:31

标签: android android-intent view

我想通过我的view更新我的view其他活动。这是我传递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);
            }
        });

然后我想在其他活动中更新我的观点

@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));
    }
}

但它显示了例外。

  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)

2 个答案:

答案 0 :(得分:3)

传递给bundle的参数应该实现SerializableParcelable接口。 LinearLayout没有。{1}}。最佳解决方案是将视图中的数据传递给Intent并将其应用于接收Activity

中的视图

答案 1 :(得分:0)

您无法在意图中传递视图(imageview)。

您应该将图像位图作为ByteArrayParcelable传递给意图,如下所示。

将位图传递为ByteArray:

首先将图片转换为ByteArray然后传入Intent,然后在下一个活动中从ByteArray获取Bundle并转换为图片(位图)并设置为ImageView。< / p>

Bitmap转换为ByteArray并转入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);

ByteArray获取Bundle并转换为Bitmap图片: -

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);

将位图传递为Parcelable:

Bitmap直接传递给Intent Parcelable extra,并在Parcelable的下一个活动中将位图作为Bundle额外获取,但问题是您的位图/图像大小是否为那时候图像没有加载到下一个活动中。

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