如何在 ImageView 中显示图库中的图像?

How to display an image from the Gallery in an ImageView?

我创建了一个按钮,让用户可以在“用相机拍照”和“Select 图片来自画廊”之间进行选择。

当图片为 taken/chosen 时,我将其显示在下一个 activity 的 ImageView 中,我通过传递为存储 taken/selected 图片而创建的文件的 URI .

当用户用他的相机拍照但当他 select 从画廊中获取图像时,尽管有两种意图(拍摄图片和 select 一张图片)编码相同。

我的问题:为什么下一个 activity 中的图像只有在从图库中选取时才显示?或者我应该如何继续显示它?

意图打开相机(工作正常):

private void openCameraToTakePictureIntent() {
    Log.d(TAG, "Method for Intent Camera started");
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }

        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.emergence.pantherapp.fileprovider", photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }
}

打算访问图库并选择图片:

private void openGalleryIntent() {
    Log.d(TAG, "Method for Intent Gallery started");
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);

    if (galleryIntent.resolveActivity(getPackageManager()) != null) {
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
        }

        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.emergence.pantherapp.fileprovider", photoFile);
            galleryIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(galleryIntent, PICK_IMAGE);
        }
    }
}

然后是onActivityResult:(currentPhotoPath是创建用来存放图片的文件的绝对路径)

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

    if (resultCode == Activity.RESULT_OK && requestCode == 1) {
        Log.d(TAG, currentPhotoPath);

        Intent intent = new Intent(this, ModifyPictureActivity.class);
        intent.putExtra("USER_IMAGE", currentPhotoPath);
        startActivity(intent);
    } else if (resultCode == Activity.RESULT_OK && requestCode == 2) {
        Log.d(TAG, currentPhotoPath);

        Intent intent = new Intent(this, ModifyPictureActivity.class);
        intent.putExtra("USER_IMAGE", currentPhotoPath);
        startActivity(intent);
    }

}

下面是图片在下面activity中的显示方式:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_modify_picture);

    Intent intent = getIntent();
    String imageUri = intent.getStringExtra("USER_IMAGE");

    if (imageUri != null) {
        Log.d(TAG, imageUri);
    } else {
        Log.d(TAG, "imageUri was null");
    }

    image = findViewById(R.id.picture);

    image.setImageURI(Uri.parse(imageUri));

}

我确保在清单中包含 READ_EXTERNAL_STORAGE,并且 xml 布局的高度和宽度仅设置为“match_parent”,但如果相关,我可以添加它们.

很少 Intent 操作使用 EXTRA_OUTPUT。大多数情况下,这是 ACTION_IMAGE_CAPTURE 的事情。

更典型的是,Intent 用于获取一段内容(ACTION_PICKACTION_GET_CONTENTACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENTACTION_OPEN_DOCUMENT_TREE等)return一个Uri from the content supplier in the Intentdelivered toonActivityResult(). Given your implementation, that would be data.getData()to get thatUri`.

然后您可以使用 ContentResolveropenInputStream()Uri 标识的内容上获得 InputStream。例如,在您的情况下,您可以使用 InputStream 将字节复制到 FileOutputStream 以制作您自己的内容本地副本。

注意 you only have short-term access to the content identified by the Uri.