简体   繁体   English

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

[英]How to display an image from the Gallery in an ImageView?

I created a button that lets the user choose between "Take picture with camera" and "Select picture from gallery".我创建了一个按钮,让用户可以在“用相机拍照”和“从图库中选择图片”之间进行选择。

When the picture is taken/chosen, I then display it in an ImageView of the next activity which I do by passing the URI of the file created to store the taken/selected picture.拍摄/选择图片后,我将其显示在下一个活动的 ImageView 中,我通过传递为存储拍摄/选择的图片而创建的文件的 URI。

It works as expected when the user takes a picture with his camera but when he selects an image from gallery, no image is shown in the next activity despite both intents (take a picture and select a picture) being coded the same.当用户使用他的相机拍照时,它按预期工作,但是当他从图库中选择图像时,尽管两种意图(拍照和选择图片)的编码相同,但下一个活动中不会显示任何图像。

My question(s): Why isn't the image displayed in the next activity ONLY when picked from the gallery ?我的问题:为什么只有从图库中挑选出来的图片才会显示在下一个活动中? Or how should I proceed to display it ?或者我应该如何继续显示它?

Intent to open camera (working fine):意图打开相机(工作正常):

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

Intent to access gallery and pick an image:意图访问图库并选择图像:

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

Then here's the onActivityResult: (currentPhotoPath is the absolute path of the file created to store the 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);
    }

}

Below is how the image is displayed in the following 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));

}

I made sure to have the READ_EXTERNAL_STORAGE in the manifest and the xml layout is just set to "match_parent" for height and width but I can add them if it's relevant.我确保在清单中有 READ_EXTERNAL_STORAGE 并且 xml 布局只是将高度和宽度设置为“match_parent”,但如果相关,我可以添加它们。

Few Intent actions use EXTRA_OUTPUT .很少有Intent操作使用EXTRA_OUTPUT Mostly, that is an ACTION_IMAGE_CAPTURE thing.大多数情况下,这是ACTION_IMAGE_CAPTURE事情。

More typically, an Intent for getting a piece of content ( ACTION_PICK , ACTION_GET_CONTENT , ACTION_OPEN_DOCUMENT , ACTION_CREATE_DOCUMENT , ACTION_OPEN_DOCUMENT_TREE , etc.) return a Uri from the content supplier in the Intent delivered to onActivityResult() . Given your implementation, that would be更典型地,一个Intent用于获取一条内容( ACTION_PICKACTION_GET_CONTENTACTION_OPEN_DOCUMENTACTION_CREATE_DOCUMENTACTION_OPEN_DOCUMENT_TREE等)返回一个Uri from the content supplier in the意图delivered to onActivityResult() . Given your implementation, that would be . Given your implementation, that would be data.getData() to get that Uri`. . Given your implementation, that would be data.getData() to get that Uri`。

You can then use a ContentResolver and openInputStream() to get an InputStream on the content identified by the Uri .然后,您可以使用ContentResolveropenInputStream()在由Uri标识的内容上获取InputStream In your case, for example, you could use that InputStream to copy the bytes to a FileOutputStream to make your own local copy of the content.例如,在您的情况下,您可以使用该InputStream将字节复制到FileOutputStream以制作您自己的内容本地副本。

Note that you only have short-term access to the content identified by the Uri .请注意, 您只能短期访问由Uri标识的内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM