java - 无法从外部存储和其他文件夹(下载除外)上传/选择 Android 11 设备中的图像和文件附件
问题描述
你好呀,
- 我有一个自定义的 android 应用程序,允许上传文件(pdf、excel、doc、图像)。
- 在使用 Android 10 之前,我可以从设备上的任何路径上传此类文件。现在我的设备已升级到 android 11,我面临上传问题。
- 我可以从“下载”文件夹中选择文件附件,但无法从 Andorid 11 设备上的其他文件夹和外部文件夹中选择任何附件。我在我的物理设备以及 Android Emulator 上都面临这个问题。如果有人在这个问题上帮助我,那将有很大帮助。
这是我的应用程序的源代码供参考
MainActivity.java
public void addFiles() {
if (AppUtils.storageInitialCheck(getActivity())) {
AppUtils.storagePermisson(getActivity());
} else {
picupFile(AppUtils.intentSelect.ADD_HOME_WORK_FILE_UPLOAD);
}
}
public void picupFile(int fileUpload) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setAction(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"), fileUpload);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
AppLog.LOGE("Exception");
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
AppLog.LOGE("onActivityResult() called with: requestCode = [" + requestCode + "], resultCode = [" + resultCode + "], data = [" + data + "]");
if (requestCode == AppStrings.uploadPic.PROFILE_PIC_SELECTION_RESULT) {
if (data != null) {
String path = data.getExtras().getString(AppStrings.uploadPic.selected_image_path);
SelectedPicModel model = new SelectedPicModel();
model.setImage_id("0");
model.setImage_path(path);
selectedPicArrayList.add(model);
AppLog.LOGE("selected_image_path--->" + path);
AppLog.LOGE("onActivityResult:111--> " + AppUtils.getFilesize(path));
adapter.notifyDataSetChanged();
updateAddpicbutton();
}
}
else if (resultCode == Activity.RESULT_OK && requestCode == AppUtils.intentSelect.ADD_HOME_WORK_FILE_UPLOAD) {
String path = "";
try {
Uri uri = data.getData();
if(uri==null)
{
Toast.makeText(getContext(),"URI is NULL",Toast.LENGTH_LONG).show();
}
else {
AppLog.LOGE("URI-------------->"+uri);
}
path = AppUtils.getfilePath(getContext(), uri);
AppLog.LOGE("FILE PATH ---->"+path);
if (path != null) {
SelectedPicModel model = new SelectedPicModel();
model.setImage_id("0");
model.setImage_path(path);
selectedPicArrayList.add(model);
AppLog.LOGE("selected_image_path--->" + path);
AppLog.LOGE("onActivityResult:111--> " + AppUtils.getFilesize(path));
adapter.notifyDataSetChanged();
updateAddpicbutton();
}
} catch (Exception e) {
e.printStackTrace();
}
}
if (requestCode == Appintegers.NotbrdStudentsActivity) {
sendstudeintIds = "";
if (data != null) {
student_array = (ArrayList<StudentsModel>) data.getSerializableExtra(AppStrings.intentData.student_array);
getSelectedIds(student_array);
if (getSelectedNames(student_array).equals("")){
selected_student_tv.setText(getResources().getString(R.string.select_students));
}else {
selected_student_tv.setText(getSelectedNames(student_array));
}
}
}
}
AppUtils.java
public static String getfilePath(final Context context, final Uri uri) {
// DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProviderString[] contentUriPrefixesToTry
else if (isDownloadsDocument(uri)) {
String fileName = getFilePath1(context, uri);
if (fileName != null) {
return Environment.getExternalStorageDirectory().toString()+"/Download/"+fileName;
}
final String id = DocumentsContract.getDocumentId(uri);
String[] contentUriPrefixesToTry = new String[]{
"content://downloads/public_downloads",
"content://downloads/my_downloads",
"content://downloads/all_downloads"
};
for (String contentUriPrefix : contentUriPrefixesToTry) {
Uri contentUri = ContentUris.withAppendedId(Uri.parse(contentUriPrefix), Long.valueOf(id));
AppLog.LOGV("contentUri---->" + contentUri);
try {
String path = getDataColumn(context, contentUri, null, null);
AppLog.LOGV("1.contentUri path---->" + path);
if (path != null) {
AppLog.LOGV("2.contentUri matched path---->" + path);
return path;
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
- 如果我尝试从 DOWNLOADS 以外的文件夹或外部存储中选择文件,则文件路径为空(请参阅 MainActivity.java 代码并在 AppLog 中也提到了文件路径)。
如果有人在这个问题上帮助我,那将是很大的帮助。
解决方案
推荐阅读
- google-cloud-platform - 将数据从 Google Cloud Compute Engine VM 获取到 Google Cloud Functions
- asp.net-core-mvc - 在扩展方法中使用 ApplicationDbContext
- javascript - 初始化服务器时看到控制台错误 - 导入未定义?
- reactjs - 从 React 应用离线访问经纬度
- java - 直接自引用导致循环
- json - 如何获取位于 ArrayType 列中的字段名称
- apache-storm - org.apache.storm.utils.NimbusLeaderNotFoundException:无法从种子主机中找到领导灵气
- ionic-framework - Ionic - 运行时错误 this.fba.logEvent(...).then 不是函数
- wordpress - 邮件黑猩猩每次都向订阅者发送所有提要
- istio - Istio Kafka Wire 协议支持