我可以创建一个可以选择图像文件的意图,但是它不允许我选择PDF:
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*|application/pdf|audio/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture PDF "), PICK_PDF_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
}
}
明确意图:
private void uploadFile() {
//if there is a file to upload
if (filePath != null) {
//displaying a progress dialog while upload is going on
Uri pdfUri = Uri.parse(String.valueOf(filePath));
Intent shareIntent = ShareCompat.IntentBuilder.from(this)
.setText("Share PDF doc")
.setType("image/*|application/pdf|audio/*")
.setStream(pdfUri)
.getIntent()
.setPackage("com.google.android.apps.docs");
startActivity(shareIntent);
}
//if there is not any file
else {
//you can display an error toast
}
}
答案 0 :(得分:0)
尝试使用FileProvider.getUriForFile
-
Uri pdfUri = FileProvider.getUriForFile(context, FILES_AUTHORITY, filePath);
Intent shareIntent = ShareCompat.IntentBuilder.from(activity)
.setStream(pdfUri)
.getIntent();
shareIntent.setData(pdfUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
还在清单中添加提供程序-
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="<your provider authority>"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_provider_paths"/>
</provider>
也-
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="shared" path="shared/"/>
</paths>