【发布时间】:2020-12-16 06:29:54
【问题描述】:
我创建了一个按钮,让用户可以在“用相机拍照”和“从图库中选择图片”之间进行选择。
当照片被拍摄/选择后,我将它显示在下一个活动的 ImageView 中,我通过传递创建的文件的 URI 来存储拍摄/选择的照片。
当用户用他的相机拍照时,它按预期工作,但是当他从图库中选择一张图片时,尽管两个意图(拍照和选择一张图片)的编码相同,但下一个活动中不会显示任何图像。
我的问题:为什么只有从图库中挑选的图像才显示在下一个活动中?或者我应该如何继续展示它?
打算打开相机(工作正常):
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);
}
}
以下是图像在以下活动中的显示方式:
@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”,但如果相关,我可以添加它们。
【问题讨论】:
-
ACTION_PICK不使用EXTRA_OUTPUT。见the documentation 和the documentation。 -
@CommonsWare 谢谢。您知道如何从我之前创建的文件中的 ACTION_PICK 返回的 URI 中复制所选图像吗?
-
使用
ContentResolver和openInputStream()在Uri标识的内容上获取InputStream。使用FileOutputStream在File标识的内容上获取OutputStream。然后,将字节从InputStream复制到OutputStream。 -
我不知道如何将此标记为有效答案。效果很好,谢谢。