hi guys I'm trying to display all doc , docx, pdf, xls, txt in a listview from my phone. so for starters, I tried this to implement on my app but somehow im having an error on this line ContentResolver cr = context.getContentResolver();
edit: thanks for answering CommonsWare. now im having a problem with this line newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
and this is my code
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(); }
Am I doing it right? please help me.
and how can I query multiple specified files like docs,docx,xls,txt? so that i can list it together with pdf.
thank you in Advance!
Context context = null;
Your first line sets context
to null
.
ContentResolver cr = context.getContentResolver();
Your second line results in a NullPointerException
, as you are trying to call getContentResolver()
on a null
Context
.
You need an actual Context
, such as your Activity
or Service
.
This syntax incorrect as you are trying to get the returned column as listed in your projection. Make sure that your projection lists the columns you want. Think of it as a spreadsheet where each column represents another attribute of the data. Your do while loop then effectively works down each row, whilst you ask for each element of that row.
incorrect:
allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
correct:
String[] projection ={ MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE} ;
and
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));
Thank you for your answer! I got a working solution by your code and have solved problem!
It works:
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;
}
Hope it help someone as me =)
I have created a query like this and it's working like a charm
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
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.