如何列出我的android设备中的所有pdf文件
我找到了关于如何获取所有图像的代码 有人能告诉我如何在内部存储和外部存储中只获取.pdf文件吗?如何列出我的android设备中的所有pdf文件,android,pdf,Android,Pdf,我找到了关于如何获取所有图像的代码 有人能告诉我如何在内部存储和外部存储中只获取.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
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]);
}
final String[]columns={MediaStore.Images.Media.DATA,MediaStore.Images.Media.\u ID};
最终字符串orderBy=MediaStore.Images.Media.\u ID;
//将库中的所有图像存储在光标中
Cursor Cursor=getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,列,null,
null,orderBy);
//图像总数
int count=cursor.getCount();
//创建一个数组来存储所有图像的路径
字符串[]arrPath=新字符串[计数];
for(int i=0;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.
}
}
}
}
}
您可以尝试以下方法:
1.)
private ListView-mListView;
私有ArrayList mAttachmentList=新ArrayList();
private ArrayList fileList=new ArrayList();
mListView=(ListView)findViewById(R.id.listAttachments);
File dir=新文件(Environment.getExternalStorageDirectory().getAbsolutePath());
获取文件(dir);
setAdapter();
(二)
public ArrayList getfile(文件目录){
文件listFile[]=dir.listFiles();
if(listFile!=null&&listFile.length>0){
for(int i=0;i
(三)
private void setAdapter()
{
AttachmentAdapter itemsAdapter=新的AttachmentAdapter(AttachmentFileList.this);
ArrayList=新建ArrayList();
itemsAdapter.setData(mAttachmentList);
mListView.setAdapter(itemsAdapter);
}
您应该能够通过Android系统列出所有这些文件,而无需手动查看设备的所有文件夹
例如:
String selection = "_data LIKE '%.pdf'"
try (Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Files.getContentUri("external"), null, selection, null, "_id DESC")) {
if (cursor== null || cursor.getCount() <= 0 || !cursor.moveToFirst()) {
// this means error, or simply no results found
return;
}
do {
// your logic goes here
} while (cursor.moveToNext());
}
String selection=“\u类似于“%.pdf”的数据”
try(Cursor Cursor=getApplicationContext().getContentResolver().query(MediaStore.Files.getContentUri(“外部”),null,selection,null,“\u id DESC”)){
if(cursor==null | | cursor.getCount()如果使用Kotlin,我认为这是最简洁的方法
val ROOT_DIR = Environment.getExternalStorageDirectory().absolutePath
val ANDROID_DIR = File("$ROOT_DIR/Android")
val DATA_DIR = File("$ROOT_DIR/data")
File(ROOT_DIR).walk()
// befor entering this dir check if
.onEnter{ !it.isHidden // it is not hidden
&& it != ANDROID_DIR // it is not Android directory
&& it != DATA_DIR // it is not data directory
&& !File(it, ".nomedia").exists() //there is no .nomedia file inside
}.filter { it.extension == "pdf" }
.toList()
您可以对java8流或RxJava执行类似的操作,您可以使用以下内容列出PDF
文档(这不会列出设备上未知的PDF)
如果您想要的不仅仅是PDF
文件,如DocX
,您可以稍微更改where
子句以满足您的需要:
String whereClause = MediaStore.Files.FileColumns.MIME_TYPE + " IN ('" + mimeType + "')"
+ " OR " + MediaStore.Files.FileColumns.MIME_TYPE + " LIKE 'application/vnd%'"
然后循环遍历光标以检索文档:
int idCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
int mimeCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE);
int addedCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_ADDED);
int modifiedCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED);
int nameCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
int titleCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
int sizeCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE);
if (cursor.moveToFirst()) {
do {
Uri fileUri = Uri.withAppendedPath(MediaStore.Files.getContentUri("external"), cursor.getString(idCol));
String mimeType = cursor.getString(mimeCol);
long dateAdded = cursor.getLong(addedCol);
long dateModified = cursor.getLong(modifiedCol);
// ...
} while (cursor.moveToNext());
}
您是否尝试过解决此问题?如果尝试过,请在问题中包含您的代码和研究,以显示哪些内容对您不起作用。如果没有,您应该先尝试自己解决,然后将代码和研究发布在此处。这也使您的问题更容易让其他人回答!可能存在重复的问题
String selection = "_data LIKE '%.pdf'"
try (Cursor cursor = getApplicationContext().getContentResolver().query(MediaStore.Files.getContentUri("external"), null, selection, null, "_id DESC")) {
if (cursor== null || cursor.getCount() <= 0 || !cursor.moveToFirst()) {
// this means error, or simply no results found
return;
}
do {
// your logic goes here
} while (cursor.moveToNext());
}
val ROOT_DIR = Environment.getExternalStorageDirectory().absolutePath
val ANDROID_DIR = File("$ROOT_DIR/Android")
val DATA_DIR = File("$ROOT_DIR/data")
File(ROOT_DIR).walk()
// befor entering this dir check if
.onEnter{ !it.isHidden // it is not hidden
&& it != ANDROID_DIR // it is not Android directory
&& it != DATA_DIR // it is not data directory
&& !File(it, ".nomedia").exists() //there is no .nomedia file inside
}.filter { it.extension == "pdf" }
.toList()
// Use android.provider.MediaStore
String[] projection = {
MediaStore.Files.FileColumns._ID,
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.DATE_MODIFIED,
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.Files.FileColumns.TITLE,
MediaStore.Files.FileColumns.SIZE,
};
String mimeType = "application/pdf";
String whereClause = MediaStore.Files.FileColumns.MIME_TYPE + " IN ('" + mimeType + "')";
String orderBy = MediaStore.Files.FileColumns.SIZE + " DESC";
Cursor cursor = getContentResolver().query(MediaStore.Files.getContentUri("external"),
projection,
whereClause,
null,
orderBy);
String whereClause = MediaStore.Files.FileColumns.MIME_TYPE + " IN ('" + mimeType + "')"
+ " OR " + MediaStore.Files.FileColumns.MIME_TYPE + " LIKE 'application/vnd%'"
int idCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns._ID);
int mimeCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE);
int addedCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_ADDED);
int modifiedCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATE_MODIFIED);
int nameCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
int titleCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE);
int sizeCol = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.SIZE);
if (cursor.moveToFirst()) {
do {
Uri fileUri = Uri.withAppendedPath(MediaStore.Files.getContentUri("external"), cursor.getString(idCol));
String mimeType = cursor.getString(mimeCol);
long dateAdded = cursor.getLong(addedCol);
long dateModified = cursor.getLong(modifiedCol);
// ...
} while (cursor.moveToNext());
}