gpt4 book ai didi

java - 使用 Intent 显示 PDF 文件选择

转载 作者:行者123 更新时间:2023-12-02 02:42:11 24 4
gpt4 key购买 nike

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

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

Activity 结果:

 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 文件

最佳答案

从移动设备获取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);
}
}
}
}
}

关于java - 使用 Intent 显示 PDF 文件选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45278795/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com