我找到了这段关于如何获取我所有图像的代码。
谁能告诉我如何在内部存储和外部存储中只获取 .pdf 文件?
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
//Stores all the images from the gallery in Cursor
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
//Total number of images
int count = cursor.getCount();
//Create an array to store path to all the images
String[] arrPath = new String[count];
for (int i = 0; i < count; i++) {
cursor.moveToPosition(i);
int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
//Store the path of the image
arrPath[i]= cursor.getString(dataColumnIndex);
Log.i("PATH", arrPath[i]);
}
最佳答案
可能的解决方案是转到每个文件夹并检查 .pdf 是否存在,如果存在,您可以对该文件做任何您想做的事情
public void Search_Dir(File dir) {
String pdfPattern = ".pdf";
File FileList[] = dir.listFiles();
if (FileList != null) {
for (int i = 0; i < FileList.length; i++) {
if (FileList[i].isDirectory()) {
Search_Dir(FileList[i]);
} else {
if (FileList[i].getName().endsWith(pdfPattern)){
//here you have that file.
}
}
}
}
}
函数调用将是
Search_Dir(Environment.getExternalStorageDirectory());
关于android - 如何列出我的 android 设备中的所有 pdf 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31652173/