android - 如何列出我的 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
    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/

相关文章:

android - runOnUiThread() 方法和@UiThreadTest 注解的区别

android - 网格图像并水平滑动屏幕以获得下一个图像网格

android - RecyclerView 中的图像不会在启动时出现

android - onclicklistener 不在方法内部工作

c# - iTextSharp - 裁剪 PDF 文件 (C#)

c# - 如何将字符串转换为pdf?

java - 如何将 "send"变量从应用程序转移到不同的模块?

带有 PDF 的 PHPMailer AddStringAttachment

pdf - 如何为时间戳签名启用 LTV?

php - 从网络服务 nusoap 接收 pdf 内容的奇怪行为