【发布时间】:2020-02-27 19:42:30
【问题描述】:
我正在尝试查找从文件选择器意图返回的 URI 的完整文件路径。我从 Internet 下载了一张图片,该图片保存在浏览器默认下载文件夹中。问题是 DocumentsContract.getDocumentId(content_describer) 返回的 id 类似于 "msf:254" 而不是通常返回的 Long 类型的 id。代码如下
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 101 && resultCode == Activity.RESULT_OK) {
Uri content_describer = data.getData();
String src = content_describer.getPath();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), data.getData());
imgView.setImageBitmap(bitmap);
}catch(Exception e){
}
Log.e("selected file", content_describer.toString());
String filename = content_describer.getLastPathSegment();
Log.e("selected path", filename);
Log.e("authority",content_describer.getAuthority());
if (content_describer.getAuthority().equals("com.android.externalstorage.documents")) {
final String docId = DocumentsContract.getDocumentId(content_describer);
final String[] split = docId.split(":");
final String type = split[0];
Log.e("npath", "getPath() docId: " + docId + ", split: " + split.length + ", type: " + type);
if ("primary".equalsIgnoreCase(type)) {
if (split.length > 1) {
Log.e("nnpath1", Environment.getExternalStorageDirectory() + "/" + split[1]);
curFile = Environment.getExternalStorageDirectory() + "/" + split[1];
} else {
Log.e("nnpath2", Environment.getExternalStorageDirectory() + "/");
}
// This is for checking SD Card
} else {
Log.e("nnpath3", "storage" + "/" + docId.replace(":", "/"));
curFile = "storage" + "/" + docId.replace(":", "/");
}
}
if (content_describer.getAuthority().equals("com.android.providers.downloads.documents")) {
final String docId = DocumentsContract.getDocumentId(content_describer);
Log.e("docid",docId);
Uri ci = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId));
final String column = "_data";
final String[] projection = {
column
};
Cursor cursor10 = getActivity().getApplicationContext().getContentResolver().query(ci, projection, null, null,
null);
if (cursor10 != null && cursor10.moveToFirst()) {
final int column_index = cursor10.getColumnIndexOrThrow(column);
Log.e("imgpp", cursor10.getString(column_index));
Log.e("imgpp2", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
File fn = new File(cursor10.getString(column_index));
Log.e("imgpp3", fn.getPath());
curFile = fn.getPath();
}
}
if (content_describer.getAuthority().equals("com.android.providers.media.documents")) {
final String docId = DocumentsContract.getDocumentId(content_describer);
Log.e("docid", docId);
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]
};
final String column = "_data";
final String[] projection = {
column
};
Cursor cursor1 = getActivity().getApplicationContext().getContentResolver().query(contentUri, projection, selection, selectionArgs,
null);
if (cursor1 != null && cursor1.moveToFirst()) {
final int column_index = cursor1.getColumnIndexOrThrow(column);
Log.e("imgpp", cursor1.getString(column_index));
Log.e("imgpp2", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getPath());
File fn = new File(cursor1.getString(column_index));
Log.e("imgpp3", fn.getPath());
curFile = fn.getPath();
}
}
Log.e("final path",curFile);
}
}
请注意,如果我通过 Image->Download 选择相同的图像,上面的代码可以正常工作,但是当从 Downloads 文件夹中选择相同的文件时,会引发以下错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.flexi.app.android, PID: 15414
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=196709, result=-1, data=Intent { dat=content://com.android.providers.downloads.documents/document/msf:253 flg=0x1 }} to activity {com.flexi.app.android/com.flexi.app.android.ProfileActivity}: java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/253
at android.app.ActivityThread.deliverResults(ActivityThread.java:4830)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4871)
at android.app.servertransaction.ActivityResultItem.execute(ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2009)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7319)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
Caused by: java.lang.IllegalArgumentException: Unknown URI: content://downloads/public_downloads/253
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
at android.content.ContentProviderProxy.query(ContentProviderNative.java:423)
at android.content.ContentResolver.query(ContentResolver.java:934)
at android.content.ContentResolver.query(ContentResolver.java:872)
at android.content.ContentResolver.query(ContentResolver.java:830)
at com.flexi.app.android.fragments.ProfileImageFragment.onActivityResult(ProfileImageFragment.java:171)
at androidx.fragment.app.FragmentActivity.onActivityResult(FragmentActivity.java:170)
at com.flexi.app.android.ProfileActivity.onActivityResult(ProfileActivity.java:28)
at android.app.Activity.dispatchActivityResult(Activity.java:8091)
URI 报告为 content://com.android.providers.downloads.documents/document/msf%3A253 知道什么是 msf 吗?我看过图像、视频、audo,但第一次注意到 msf。
【问题讨论】:
-
The problem is the id returned by DocumentsContract.getDocumentId(content_describer) is like "msf:254" instead of Long type id generally returned.长?该函数总是返回一个字符串。特别是在某些设备上,下载目录的行为与主或 SD 卡部分不同。对于单独的下载;加载部分“msd:254”是可以的。查询 DISPLAY_NAME 的内容解析器。 -
无论如何您都试图从该内容方案中获取经典文件路径。你甚至不应该尝试这样做。你需要什么文件路径?如果您想读取文件,可以直接从内容方案中读取。
-
IllegalArgumentException: Unknown URI: content://downloads/public_downloads/253这确实是一个不可能的内容方案。你虐待了最初获得的方案,可能看起来像conten://com.android.providers.downloads.documents/document/253。 -
Uri ci = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(docId)); Remove that line and do not query(ci, but the original uri. -
I am trying to find the full file path for an URI returned from file picker intent.??我认为您正在尝试从获得的 uri 中找到经典文件路径。为什么 ?你需要那个文件路径做什么?您可以使用内容方案完成所有工作。不要在这个糟糕的代码上花费更多时间,并直接学习使用内容方案。很快你就有了 Android Q,然后你应该使用 Storage Access Framework。
标签: android