大家好我试图在手机列表视图中显示所有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一起列出。
提前谢谢你!
答案 0 :(得分:0)
Context context = null;
您的第一行将context
设置为null
。
ContentResolver cr = context.getContentResolver();
您的第二行会生成NullPointerException
,因为您尝试在getContentResolver()
null
上致电Context
。
您需要一个实际的Context
,例如Activity
或Service
。
答案 1 :(得分:0)
当您尝试获取投影中列出的返回列时,此语法不正确。确保投影列出了所需的列。可以将其视为电子表格,其中每列代表数据的另一个属性。然后你的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));
答案 2 :(得分:0)
感谢您的回答! 我的代码得到了一个可行的解决方案并解决了问题!
有效:
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;
}
希望有人帮助我=)
答案 3 :(得分:0)
我已经创建了一个这样的查询,它的运行就像一个超级按钮
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
)