问题描述
我创建了一个按钮,使用户可以在“用相机拍照”和“从图库中选择图片”之间进行选择。
拍照/选择照片后,我将其传递到下一个活动的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,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);
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”,但是如果相关,我可以添加它们。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)