Я использую этот код для представления пользователю намерения выбора документа:
if (Build.VERSION.SDK_INT >= 23) {
if (ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
&& ActivityCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
) {
requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE
},
1);
}
}
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("file/*");
String[] mimetypes = {"application/msword", "application/pdf", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"};
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
startActivityForResult(intent, REQUEST_CODE);
, но это дает мне исключение:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66770, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/834 flg=0x43 }} to activity {com.mypackage/com.mypackage.activities.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.downloads.DownloadProvider uri content://downloads/all_downloads/834 from pid=22086, uid=10311 requires android.permission.ACCESS_ALL_DOWNLOADS, or grantUriPermission()
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.downloads.DownloadProvider uri content://downloads/all_downloads/834 from pid=22086, uid=10311 requires android.permission.ACCESS_ALL_DOWNLOADS, or grantUriPermission()
я попытался изменить эту строку
requestPermissions(new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
до
ActivityCompat.requestPermissions(getActivity(),new String[]{android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE,"android.permission.ACCESS_ALL_DOWNLOADS"},1;
и вот мой onActivityResult
public void onActivityResult(int requestCode, int resultCode, Intent data) {
case REQUEST_CODE:
Uri uri = null;
File imageFile = getTempFile(getContext());
boolean isCamera = (data == null ||
data.getData() == null ||
data.getData().toString().contains(imageFile.toString()));
if (isCamera) { /** CAMERA **/
uri = Uri.fromFile(imageFile);
} else { /** ALBUM **/
uri = data.getData();
}
//Uri uri = data.getData();
Log.e("Uri-", "" + uri);
int hasWriteStoragePermission = ContextCompat.checkSelfPermission(getContext(), "android.permission.ACCESS_ALL_DOWNLOADS");
final int REQUEST_CODE_ASK_PERMISSIONS = 123;
String strPath = "";
if (hasWriteStoragePermission != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) getContext(), new String[] {"android.permission.ACCESS_ALL_DOWNLOADS",
android.Manifest.permission.READ_EXTERNAL_STORAGE, android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_CODE_ASK_PERMISSIONS);
strPath = CommonUtils.getRealPathFromURI(getContext(), uri);
}
if (strPath != null) {
Log.e("strPath", "" + strPath);
if (strPath.endsWith("pdf") || strPath.endsWith("doc") || strPath.endsWith("docx")) {
File file = new File(strPath);
// tvResumeName.setText(file.getName());
Log.e("file", file + "");
Log.e("File file", file.getAbsolutePath() + " " + file.getName());
if (CommonUtils.isInternetConnection((Activity) getContext())) {
uploadDocuments(file);
} else {
CommonDialog.With(getActivity()).Show(getString(R.string.internet_err));
}
} else {
CommonDialog.With(getActivity()).Show(getString(R.string.file_err));
}
} else {
CommonDialog.With(getActivity()).Show(getString(R.string.file_phn_err));
}
break;
}
}
я получаю ошибку при попытке запустить этот код:
getDataColumn(context, contentUri , null, null)
на основе этого вопроса, но исключение все еще сохраняется, я также просмотрел другие связанные вопросы, но безуспешно.