data.getData() 在 OnActvityResult 中返回 null,同时从相机和图库中获取/选择图片
Posted
技术标签:
【中文标题】data.getData() 在 OnActvityResult 中返回 null,同时从相机和图库中获取/选择图片【英文标题】:data.getData() returns null in OnActvityResult while taking /Selecting Pictures from Camera and Gallery 【发布时间】:2017-09-26 20:11:19 【问题描述】:我正在开发一个 android 应用程序,它有一个个人资料片段,用户可以通过拍照或从图库中选择上传个人资料图片。目前,通过 Activity.RESULT_OK 一切正常。但问题是 onActivityResult data.getData() 什么都不返回。我对这个问题进行了研究,但无济于事。
这是我的 editprofile 片段中包含的两个方法。
private void setGalleryBtn()
if (PermissionHandler.checkPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE))
AppHelper.LogCat("Read data permission already granted.");
new PickerBuilder(getActivity(), PickerBuilder.SELECT_FROM_GALLERY)
.setOnImageReceivedListener(imageUri ->
Intent data = new Intent();
data.setData(imageUri);
AppHelper.LogCat("new image SELECT_FROM_GALLERY" + imageUri);
mEditProfilePresenter.onActivityResult(this, AppConst.SELECT_PROFILE_PICTURE, RESULT_OK, data);
)
.setImageName(getActivity().getString(R.string.app_name))
.setImageFolderName(getActivity().getString(R.string.app_name))
.setCropScreenColor(R.color.colorPrimary)
.withTimeStamp(false)
.setOnPermissionRefusedListener(() ->
PermissionHandler.requestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
)
.start();
else
AppHelper.LogCat("Please request Read data permission.");
PermissionHandler.requestPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
private void setCameraBtn()
if (PermissionHandler.checkPermission(getActivity(), Manifest.permission.CAMERA))
AppHelper.LogCat("camera permission already granted.");
new PickerBuilder(getActivity(), PickerBuilder.SELECT_FROM_CAMERA)
.setOnImageReceivedListener(imageUri ->
AppHelper.LogCat("new image SELECT_FROM_CAMERA " + imageUri);
Intent data = new Intent();
data.setData(imageUri);
mEditProfilePresenter.onActivityResult(this, AppConst.SELECT_PROFILE_CAMERA, RESULT_OK, data);
)
.setImageName(getActivity().getString(R.string.app_name))
.setImageFolderName(getActivity().getString(R.string.app_name))
.setCropScreenColor(R.color.colorPrimary)
.withTimeStamp(false)
.setOnPermissionRefusedListener(() ->
PermissionHandler.requestPermission(getActivity(), Manifest.permission.CAMERA);
)
.start();
else
AppHelper.LogCat("Please request camera permission.");
PermissionHandler.requestPermission(getActivity(), Manifest.permission.CAMERA);
这是我的 EditProfilePresenter.onActivityResult 方法。
public void onActivityResult(Edit_profile_fragment myEdit_profile_fragment, int requestCode, int resultCode, Intent data)
String imagePath = null;
if (resultCode == Activity.RESULT_OK)
if (PermissionHandler.checkPermission(myEdit_profile_fragment.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE))
AppHelper.LogCat("Read contact data permission already granted.");
switch (requestCode)
case AppConst.SELECT_PROFILE_PICTURE:
imagePath = FilesManager.getPath(myEdit_profile_fragment.getActivity(), data.getData());
break;
case AppConst.SELECT_PROFILE_CAMERA:
if (data.getData() != null)
imagePath = FilesManager.getPath(myEdit_profile_fragment.getActivity(), data.getData());
else
try
String[] projection = new String[]MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA, MediaStore
.Images.ImageColumns.BUCKET_DISPLAY_NAME, MediaStore.Images.ImageColumns.DATE_TAKEN, MediaStore.Images
.ImageColumns.MIME_TYPE;
final Cursor cursor = myEdit_profile_fragment.getActivity().getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, MediaStore.Images.ImageColumns
.DATE_TAKEN + " DESC");
if (cursor != null && cursor.moveToFirst())
String imageLocation = cursor.getString(1);
cursor.close();
File imageFile = new File(imageLocation);
if (imageFile.exists())
imagePath = imageFile.getPath();
catch (Exception e)
AppHelper.LogCat("error" + e);
break;
if (imagePath != null)
EventBus.getDefault().post(new Pusher(AppConst.EVENT_BUS_IMAGE_PROFILE_PATH, imagePath));
else
AppHelper.LogCat("imagePath is null");
else
AppHelper.LogCat("Please request Read contact data permission.");
PermissionHandler.requestPermission(myEdit_profile_fragment.getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE);
请问我哪里错了?
【问题讨论】:
您找到解决方案了吗? 【参考方案1】:你可以试试这个
if (requestCode == REQUEST_FROM_CAMERA && resultCode == RESULT_OK)
Bundle extras2 = data.getExtras();
if (extras2 != null)
// do your stuff here
else
// handle this case as well if data.getExtras() is null
Uri selectedImage = data.getData();
希望对你有帮助
【讨论】:
以上是关于data.getData() 在 OnActvityResult 中返回 null,同时从相机和图库中获取/选择图片的主要内容,如果未能解决你的问题,请参考以下文章
onActivityResult 返回 Intent data.getData();仅在棉花糖和棒棒糖中始终为空
onActivityResult 返回 Intent data.getData();仅在棉花糖和棒棒糖中始终为空