片段
String action = "android.media.action.IMAGE_CAPTURE";
try {
Intent intent = new Intent(action);
myPhotoFile = new File(myPhotoTempDir, "myphoto_" + System.currentTimeMillis());
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(myPhotoFile));
getActivity().startActivityForResult(intent, TAKE_PHOTO);
此代码在Android 6.0中成功运行 但是在Android 9上,我在此行中遇到运行时错误:
getActivity().startActivityForResult(intent, TAKE_PHOTO);
错误:
android.os.FileUriExposedException: file:///storage/emulated/0/myphoto_149ed0307b49eb17/myphoto_1562770175665 exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1996)
at android.net.Uri.checkFileUriExposed(Uri.java:2378)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:963)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10425)
at android.content.Intent.prepareToLeaveProcess(Intent.java:10410)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1744)
at android.app.Activity.startActivityForResult(Activity.java:4781)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:676)
at android.app.Activity.startActivityForResult(Activity.java:4721)
at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:663)
at com.myproject.profile.EditProfileFragment.takePhoto(EditProfileFragment.java:489)