英文:
How can i get PDF Real path from Download Folder in Android?
问题
我想从下载文件夹中下载的 PDF 的 URI 中获取真实路径。
我已经在这个链接中进行了操作。但是在这个链接中,我没有获得真实路径,而且出现了异常,如下所示:
URI
> content://com.android.providers.downloads.documents/document/msf%3A26
启动意图:
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select PDF"), PICK_PDF_REQUEST);
OnActivityResult:
if (requestCode == PICK_PDF_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String path = null;
try {
path = getPDFPath(uri);
LogUtils.logE(TAG, "Document : " + path);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
private String getPDFPath(Uri uri) {
// DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
String documentId = DocumentsContract.getDocumentId(uri);
String[] split = documentId.split(":");
String type = split[0];
return Environment.getExternalStorageDirectory().toString() + "/" + split[1];
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
String decodedURI = Uri.decode(uri.toString());
if (decodedURI.contains("raw:")) {
return decodedURI.substring(decodedURI.indexOf("raw:") + 4);
}
String id = DocumentsContract.getDocumentId(Uri.parse(decodedURI));
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id));
return getDataColumn(contentUri, null, null);
}
}
return null;
}
public String getDataColumn(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 index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
异常
2020-05-04 14:47:09.747 17908-17908/ W/System.err: java.lang.NumberFormatException: For input string: "msf:26"
2020-05-04 14:47:09.748 17908-17908/W/System.err: at java.lang.Long.parseLong(Long.java:594)
2020-05-04 14:47:09.748 17908-17908/ W/System.err: at java.lang.Long.valueOf(Long.java:808)
我想获得 PDF 的真实路径,以便于后续使用。
提前谢谢!!!
英文:
I want to get the real path of PDF from URI of the downloaded pdf from the download folder.
I am already following this link. But in this, I do not get the real path and getting exception. like below:
URI
> content://com.android.providers.downloads.documents/document/msf%3A26
Lunch Intent:
Intent intent = new Intent();
intent.setType("application/pdf");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select PDF"), PICK_PDF_REQUEST);
OnActivityResult:
if (requestCode == PICK_PDF_REQUEST) {
if (resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
String path = null;
try {
path = getPDFPath(uri);
LogUtils.logE(TAG, "Document : " + path);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
private String getPDFPath(Uri uri) {
// DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
String documentId = DocumentsContract.getDocumentId(uri);
String[] split = documentId.split(":");
String type = split[0];
return Environment.getExternalStorageDirectory().toString() + "/" + split[1];
} else if ("com.android.providers.downloads.documents".equals(uri.getAuthority())) {
String decodedURI = Uri.decode(uri.toString());
if (decodedURI.contains("raw:")) {
return decodedURI.substring(decodedURI.indexOf("raw:") + 4);
}
String id = DocumentsContract.getDocumentId(Uri.parse(decodedURI));
Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), java.lang.Long.valueOf(id));
return getDataColumn(contentUri, null, null);
}
}
return null;
}
public String getDataColumn(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 index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
Exception
2020-05-04 14:47:09.747 17908-17908/ W/System.err: java.lang.NumberFormatException: For input string: "msf:26"
2020-05-04 14:47:09.748 17908-17908/W/System.err: at java.lang.Long.parseLong(Long.java:594)
2020-05-04 14:47:09.748 17908-17908/ W/System.err: at java.lang.Long.valueOf(Long.java:808)
I want Real path of PDF so I can use it for further use.
Thanks in Advance!!!
答案1
得分: 2
不需要获取“真实”路径。
没有必要。
直接使用 URI。
现在我们都是这样做的。
加入我们。
英文:
No need to get a 'real' path.
There is no need for it.
Just use the uri as is.
We all do like this these days.
Join us.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论