如果未选择图像,如何将默认图像传递给上一个活动

时间:2016-01-17 18:08:12

标签: java android image android-camera

在我的应用程序中,我有一个相机功能。用户可以捕捉图像或从图库中选择。

活动B

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                    selectedImage = data.getData();
                    imageView.setImageURI(selectedImage);
                }

                break;

            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    try {
                        selectedImage = imageUri;
                        getContentResolver().notifyChange(selectedImage, null);
                        imageView.setImageURI(null);
                        imageView.setImageURI(imageUri);
                    } catch (Exception e) {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                                .show();

                    }
                }
        }
    }

 submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent returnIntent = new Intent();
                if(selectedImage!=null) {
                    returnIntent.putExtra("img_uri", selectedImage.toString());
                }
                setResult(Activity.RESULT_OK, returnIntent);
                finish();
            }
        });

点击提交按钮后,它将返回上一个活动。

活动A

  @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (resultCode == RESULT_OK) {
            if (requestCode == PROJECT_REQUEST_CODE) {
                imgURI = Uri.parse(data.getStringExtra("img_uri"));
                if (mClickedPosition == -1) { 
                    if (obj != null)
                        obj.addNewItem(imgURI);
                } else {
                    if (obj != null)
                     obj.changeItem(mClickedPosition, imgURI);
                }
            }
        }
    }
  

我现在的问题是如何将默认图像传递给Activity A If   用户没有选择任何图像?

这是活动B

中的xml imageView
  <ImageView
                android:paddingTop="30dp"
                android:layout_gravity="center"
                android:layout_width="330dp"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true"
                android:src="@mipmap/no_image"
                android:scaleType="fitXY"
                android:id="@+id/imageView"
                android:layout_weight="0.50" />

如果没有选择图像,我希望@mipmap/no_image返回活动B.是否可能?感谢。

4 个答案:

答案 0 :(得分:4)

您可以通过下一种方式传递存储在资源中的图像的Uri:

submit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent returnIntent = new Intent();
        if(selectedImage!=null) {
            returnIntent.putExtra("img_uri", selectedImage.toString());
        } else {
            returnIntent.putExtra("img_uri", Uri.parse("android.resource://your.package.name/mipmap/no_image").toString());
        }
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }
});

然后您可以像往常一样使用它来显示ImageView中的图像。

答案 1 :(得分:1)

ByDefault将selectedImage的值设置为“默认”。

现在在addNewItem()中传递的changeItem()imageUri检查值

if(imageUri.equalsIgnoreCase("default"))
    // set your default image that is @mipmap/no_image
else
    // set image you are getting from imageUri

答案 2 :(得分:0)

代码:

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK & null != data) {
                    selectedImage = data.getData();
                    imageView.setImageURI(selectedImage);
                }else{
                   //set default
                }

                break;

            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    try {
                        selectedImage = imageUri;
                        getContentResolver().notifyChange(selectedImage, null);
                        imageView.setImageURI(null);
                        imageView.setImageURI(imageUri);
                    } catch (Exception e) {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
                                .show();
                    }
                }else{
                     //set default
                }
          default{ 
                     //set default 
                 }
    }

答案 3 :(得分:0)

您不需要将默认图像返回到活动A.像其他人写的那样,您只需在用户不选择任何图像时做好准备。 修改活动A的onActivityResult,如下所示:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if(data!=null){
        switch (requestCode) {
            case RESULT_LOAD_IMAGE:
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
                    selectedImage = data.getData();
                    imageView.setImageURI(selectedImage);
                }
                break;

            case REQUEST_IMAGE_CAPTURE:
                if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
                    try {
                        selectedImage = imageUri;
                        getContentResolver().notifyChange(selectedImage, null);
                        imageView.setImageURI(imageUri);
                    } catch (Exception e) {
                        Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT).show();
                    }
                }
                break;

            default:
            //Your default image goes here, when the user doesn't select either of the two options
            imageView.setImageResource(R.drawable.<default_image>);
                break;
        }
 }