如何使用 java 从 Android 10 中的外部存储中获取所有带有 FileName 的 PDF 文件 Uri
How to get all PDF files Uri with FileName from external storage in Android 10 using java
我正在尝试列出应用程序创建的外部存储中的所有 PDF 文件。遍历光标后如何在App中列出pdf文件?
private void getExternalPDFFiles() {
ContentResolver cr = getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;
// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE;
String[] selectionArgs = null; // there is no ? in selection so null here
String sortOrder = null; // unordered
// Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);
// only pdf
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{mimeType};
Cursor cursor = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
assert cursor != null;
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
//your code to implement
Log.d("TAG", cursor.getColumnNames().toString());
cursor.moveToNext();
}
}
Log.d("TAG", cursor.getCount() + "");
Log.d("TAG", cursor.getColumnCount() + "");
cursor.close();
}
获取Android-10及以上所有包含Uri和FileName的文件列表:
class FileModel {
String displayName;
Uri fileUri;
FileModel(String displayName, Uri fileUri) {
this.displayName = displayName;
this.fileUri = fileUri;
}
}
...
private ArrayList<FileModel> getExternalPDFFileList() {
ContentResolver cr = getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = {MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DISPLAY_NAME};
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE;
String[] selectionArgs = null;
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{mimeType};
Cursor cursor = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, null);
assert cursor != null;
ArrayList<FileModel> uriList = new ArrayList<>();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
int columnIndex = cursor.getColumnIndex(projection[0]);
long fileId = cursor.getLong(columnIndex);
Uri fileUri = Uri.parse(uri.toString() + "/" + fileId);
String displayName = cursor.getString(cursor.getColumnIndex(projection[1]));
uriList.add(new FileModel(displayName, fileUri));
}
cursor.close();
return uriList;
}
我正在尝试列出应用程序创建的外部存储中的所有 PDF 文件。遍历光标后如何在App中列出pdf文件?
private void getExternalPDFFiles() {
ContentResolver cr = getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
// every column, although that is huge waste, you probably need
// BaseColumns.DATA (the path) only.
String[] projection = null;
// exclude media files, they would be here also.
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE;
String[] selectionArgs = null; // there is no ? in selection so null here
String sortOrder = null; // unordered
// Cursor allNonMediaFiles = cr.query(uri, projection, selection, selectionArgs, sortOrder);
// only pdf
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{mimeType};
Cursor cursor = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
assert cursor != null;
if (cursor.moveToFirst()) {
while (!cursor.isAfterLast()) {
//your code to implement
Log.d("TAG", cursor.getColumnNames().toString());
cursor.moveToNext();
}
}
Log.d("TAG", cursor.getCount() + "");
Log.d("TAG", cursor.getColumnCount() + "");
cursor.close();
}
获取Android-10及以上所有包含Uri和FileName的文件列表:
class FileModel {
String displayName;
Uri fileUri;
FileModel(String displayName, Uri fileUri) {
this.displayName = displayName;
this.fileUri = fileUri;
}
}
...
private ArrayList<FileModel> getExternalPDFFileList() {
ContentResolver cr = getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection = {MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DISPLAY_NAME};
String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
+ MediaStore.Files.FileColumns.MEDIA_TYPE;
String[] selectionArgs = null;
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{mimeType};
Cursor cursor = cr.query(uri, projection, selectionMimeType, selectionArgsPdf, null);
assert cursor != null;
ArrayList<FileModel> uriList = new ArrayList<>();
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
int columnIndex = cursor.getColumnIndex(projection[0]);
long fileId = cursor.getLong(columnIndex);
Uri fileUri = Uri.parse(uri.toString() + "/" + fileId);
String displayName = cursor.getString(cursor.getColumnIndex(projection[1]));
uriList.add(new FileModel(displayName, fileUri));
}
cursor.close();
return uriList;
}