如何在 Android 的列表视图中显示存储中的 doc、docx、pdf、xls、txt
how to display doc, docx, pdf, xls, txt from storage in listview in Android
大家好,我正在尝试在 phone 的列表视图中显示所有 doc、docx、pdf、xls、txt。所以对于初学者来说,我尝试 this 在我的应用程序上实现,但不知何故我在这一行 ContentResolver cr = context.getContentResolver();
上遇到了错误
编辑:感谢您回答 CommonsWare。现在我对这条线有问题 newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
这是我的代码
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection ={ MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE} ;
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{ mimeType };
String sortOrder = null;
Cursor allPdfFiles = getContentResolver().query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
if (allPdfFiles.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = allPdfFiles.getInt(allPdfFiles.getColumnIndex(MediaStore.Files.FileColumns._ID));
newVVI.filePath = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
newVVI.title = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE));
newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
ImageRows.add(newVVI);
} while (cursor.moveToNext());
allPdfFiles.close(); }
我做的对吗?请帮助我。
以及如何查询多个指定文件,如 docs、docx、xls、txt?以便我可以将其与 pdf 一起列出。
提前致谢!
Context context = null;
您的第一行将 context
设置为 null
。
ContentResolver cr = context.getContentResolver();
你的第二行结果是 NullPointerException
,因为你试图在 null
Context
.
上调用 getContentResolver()
您需要一个实际的 Context
,例如您的 Activity
或 Service
。
此语法不正确,因为您正在尝试获取投影中列出的返回列。确保您的投影列出了您想要的列。将其视为电子表格,其中每一列代表数据的另一个属性。然后,您的 do while 循环会有效地处理每一行,同时您请求该行的每个元素。
不正确:
allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
正确:
String[] projection ={ MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE} ;
和
newVVI.filePath = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
newVVI.title = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE));
newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE));
感谢您的回答!
我通过你的代码得到了一个有效的解决方案并且已经解决了问题!
有效:
private static final String orderBy = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
final String[] columns = {
MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.MIME_TYPE};
List<String> extensions = new ArrayList<>();
extensions.add("pdf");
extensions.add("csv");
extensions.add("doc");
extensions.add("docx");
extensions.add("xls");
extensions.add("xlsx");
List<String> mimes = new ArrayList<>();
for (String ext : extensions) {
mimes.add(MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext));
}
Cursor cursor;
cursor = contentResolver.query(uri, columns, null, null, orderBy);
if (cursor != null) {
int mimeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
int pathColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
while (cursor.moveToNext()) {
String mimeType = cursor.getString(mimeColumnIndex);
String filePath = cursor.getString(pathColumnIndex);
if (mimeType != null && mimes.contains(mimeType)) {
// handle cursor
makeFile(cursor);
} else {
// need to check extension, because the Mime Type is null
String extension = getExtensionByPath(filePath);
if (extensions.contains(extension)) {
// handle cursor
makeFile(cursor);
}
}
}
cursor.close();
}
public static String getExtensionByPath(@NonNull String path) {
String result = "%20";
int i = path.lastIndexOf('.');
if (i > 0) {
result = path.substring(i + 1);
}
return result;
}
private MyCustomFile makeFile(Cursor cursor) {
int mimeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
int pathColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int sizeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
int titleColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.TITLE);
int nameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
int fileId = cursor.getInt(pathColumnIndex);
String fileSize = cursor.getString(sizeColumnIndex);
String fileDisplayName = cursor.getString(nameColumnIndex);
String fileTitle = cursor.getString(titleColumnIndex);
String filePath = cursor.getString(pathColumnIndex);
String mimeType = cursor.getString(mimeColumnIndex);
String type = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
if (type == null) {
type = FileUtils.getExtensionByPath(filePath);
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(type);
}
MyCustomFile result = new MyCustomFile();
result.setFileId(fileId);
result.setFilePath(filePath);
result.setFileSize(fileSize);
result.setFileTitle(fileTitle);
result.setFileDisplayName(fileDisplayName);
result.setMimeType(mimeType);
result.setFileExtension(type);
return result;
}
希望对像我这样的人有所帮助=)
我创建了一个这样的查询,它运行得非常好
val mimeTypes = mutableListOf<String>()
val extenstions = mutableListOf("pdf","doc","odt")
extenstions.forEach { mimeType ->
MimeTypeMap
.getSingleton()
.getMimeTypeFromExtension(mimeType)?.let {
mimeTypes.add("'$it'")
}
}
val selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + " IN (${mimeTypes.joinToString()})"
context.contentResolver.query(
MediaStore.Files.getContentUri("external"),
projection,
selectionMimeType,
null,
MediaStore.Video.Media.DATE_ADDED
)
大家好,我正在尝试在 phone 的列表视图中显示所有 doc、docx、pdf、xls、txt。所以对于初学者来说,我尝试 this 在我的应用程序上实现,但不知何故我在这一行 ContentResolver cr = context.getContentResolver();
编辑:感谢您回答 CommonsWare。现在我对这条线有问题 newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
这是我的代码
Uri uri = MediaStore.Files.getContentUri("external");
String[] projection ={ MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE} ;
String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
String[] selectionArgsPdf = new String[]{ mimeType };
String sortOrder = null;
Cursor allPdfFiles = getContentResolver().query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);
if (allPdfFiles.moveToFirst()) {
do {
ImageViewInfo newVVI = new ImageViewInfo();
int id = allPdfFiles.getInt(allPdfFiles.getColumnIndex(MediaStore.Files.FileColumns._ID));
newVVI.filePath = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
newVVI.title = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE));
newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
ImageRows.add(newVVI);
} while (cursor.moveToNext());
allPdfFiles.close(); }
我做的对吗?请帮助我。
以及如何查询多个指定文件,如 docs、docx、xls、txt?以便我可以将其与 pdf 一起列出。
提前致谢!
Context context = null;
您的第一行将 context
设置为 null
。
ContentResolver cr = context.getContentResolver();
你的第二行结果是 NullPointerException
,因为你试图在 null
Context
.
getContentResolver()
您需要一个实际的 Context
,例如您的 Activity
或 Service
。
此语法不正确,因为您正在尝试获取投影中列出的返回列。确保您的投影列出了您想要的列。将其视为电子表格,其中每一列代表数据的另一个属性。然后,您的 do while 循环会有效地处理每一行,同时您请求该行的每个元素。
不正确:
allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
正确:
String[] projection ={ MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE} ;
和
newVVI.filePath = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
newVVI.title = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE));
newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE));
感谢您的回答! 我通过你的代码得到了一个有效的解决方案并且已经解决了问题!
有效:
private static final String orderBy = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";
ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");
final String[] columns = {
MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.MIME_TYPE};
List<String> extensions = new ArrayList<>();
extensions.add("pdf");
extensions.add("csv");
extensions.add("doc");
extensions.add("docx");
extensions.add("xls");
extensions.add("xlsx");
List<String> mimes = new ArrayList<>();
for (String ext : extensions) {
mimes.add(MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext));
}
Cursor cursor;
cursor = contentResolver.query(uri, columns, null, null, orderBy);
if (cursor != null) {
int mimeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
int pathColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
while (cursor.moveToNext()) {
String mimeType = cursor.getString(mimeColumnIndex);
String filePath = cursor.getString(pathColumnIndex);
if (mimeType != null && mimes.contains(mimeType)) {
// handle cursor
makeFile(cursor);
} else {
// need to check extension, because the Mime Type is null
String extension = getExtensionByPath(filePath);
if (extensions.contains(extension)) {
// handle cursor
makeFile(cursor);
}
}
}
cursor.close();
}
public static String getExtensionByPath(@NonNull String path) {
String result = "%20";
int i = path.lastIndexOf('.');
if (i > 0) {
result = path.substring(i + 1);
}
return result;
}
private MyCustomFile makeFile(Cursor cursor) {
int mimeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
int pathColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
int sizeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
int titleColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.TITLE);
int nameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);
int fileId = cursor.getInt(pathColumnIndex);
String fileSize = cursor.getString(sizeColumnIndex);
String fileDisplayName = cursor.getString(nameColumnIndex);
String fileTitle = cursor.getString(titleColumnIndex);
String filePath = cursor.getString(pathColumnIndex);
String mimeType = cursor.getString(mimeColumnIndex);
String type = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
if (type == null) {
type = FileUtils.getExtensionByPath(filePath);
mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(type);
}
MyCustomFile result = new MyCustomFile();
result.setFileId(fileId);
result.setFilePath(filePath);
result.setFileSize(fileSize);
result.setFileTitle(fileTitle);
result.setFileDisplayName(fileDisplayName);
result.setMimeType(mimeType);
result.setFileExtension(type);
return result;
}
希望对像我这样的人有所帮助=)
我创建了一个这样的查询,它运行得非常好
val mimeTypes = mutableListOf<String>()
val extenstions = mutableListOf("pdf","doc","odt")
extenstions.forEach { mimeType ->
MimeTypeMap
.getSingleton()
.getMimeTypeFromExtension(mimeType)?.let {
mimeTypes.add("'$it'")
}
}
val selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + " IN (${mimeTypes.joinToString()})"
context.contentResolver.query(
MediaStore.Files.getContentUri("external"),
projection,
selectionMimeType,
null,
MediaStore.Video.Media.DATE_ADDED
)