我正在开发一个 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);
}
}
}
请问我哪里错了?