The following code shows all files but I select another format file it returns error.
Intent selectionintent = new Intent(Intent.ACTION_GET_CONTENT);
selectionintent.setType("application/pdf");
selectionintent.addCategory(Intent.CATEGORY_OPENABLE);
PackageManager packageManager = getPackageManager();
List activitiesPDF = packageManager.queryIntentActivities(selectionintent,
PackageManager.MATCH_DEFAULT_ONLY);
boolean isIntentSafePDF = activitiesPDF.size() > 0;
if (isIntentSafePDF)
startActivityForResult(selectionintent, CODE_RESULT);
else
Toast.makeText(UpdateFileActivity.this, "Only select pdf files", Toast.LENGTH_SHORT).show();
onActivity Result:
if (requestCode == CODE_RESULT && resultCode == RESULT_OK) {
Uri uri = data.getData();
String file = uri.toString();
File f = new File(file);
if (file.startsWith("content://")) {
Cursor cursor = null;
try {
cursor = getContentResolver().query(uri, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
} finally {
cursor.close();
}
} else if (file.startsWith("file://")) {
displayName = f.getName();
}
filetext.setText(displayName);
}
I want to show only pdf files
Get the list of pdf files from mobile (I used intent but it not getting all the pdf files so I used this approach)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean result = marshMallowPermission.checkPermissionForReadExtertalStorage();
if (result) {
getPdfFromExternalStorage(Environment.getExternalStorageDirectory());
} else {
try {
marshMallowPermission.requestPermissionForReadExtertalStorage();
} catch (Exception e) {
e.printStackTrace();
}
}
} else {
getPdfFromExternalStorage(Environment.getExternalStorageDirectory());
}
Search pdf files from device
private void getPdfFromExternalStorage(File folder) {
if (folder != null) {
if (folder.listFiles() != null) {
for (File file : folder.listFiles()) {
if (file.isFile()) {
//.pdf files
if (file.getName().contains(".pdf")) {
Log.d("filePath-------", "" + file.getPath());
}
} else {
getPdfFromExternalStorage(file);
}
}
}
}
}
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.