【问题标题】:Show PDF File Selection using Intent使用 Intent 显示 PDF 文件选择
【发布时间】:2017-07-24 10:56:23
【问题描述】:

以下代码显示了所有文件,但我选择了另一个返回错误的格式文件。

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 结果:

 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);
    }

我只想显示 pdf 文件

【问题讨论】:

  • akhilesh 代码在文件名包含 .pdf 时有效,文件没有扩展名则无法工作

标签: java android pdf android-intent


【解决方案1】:

从移动设备获取 pdf 文件列表(我使用了 intent,但它没有获取所有 pdf 文件,所以我使用了这种方法)

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());
}

从设备中搜索 pdf 文件

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);
                }
            }
        }
    }
}

【讨论】:

  • if (file.getName().contains(".pdf")) { Log.d("filePath-------", "" + file.getPath()); } 已经试过了,一些 pdf 文件没有扩展名
  • 如果你不是我的,我有一个疑问:你为什么使用 M(marshmallow) 权限。我在内部存储中的文件
  • 哦,k。我的文件没有扩展名
猜你喜欢
  • 1970-01-01
  • 2015-11-20
  • 2011-09-09
  • 1970-01-01
  • 2016-06-30
  • 1970-01-01
  • 2012-03-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多