Onu*_*rım 9 pdf android android-intent android-pendingintent android-fileprovider
我正在我的应用程序上创建一个PDF文件,并将其写入外部存储"下载"目录.现在,当我通过我的应用程序通过intent action.VIEW使用FileProvider uri打开它时,Google PDF Viewer显示一个空白屏幕,Adobe Acrobat甚至无法打开该文件.这是我编写文件然后尝试显示它的代码.请注意,我正在发送本地通知并尝试通过通知打开文件.
try {
File mypath=new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
document.writeTo(new FileOutputStream(mypath));
document.close();
NotificationManager notificationManager = (NotificationManager)getContext().getSystemService(Context.NOTIFICATION_SERVICE);
File file = new File( Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),mParam1.getEmlak_id()+".pdf");
Uri pdfUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".com.onur.emlakdosyasi.provider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(pdfUri, "application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
//use the flag FLAG_UPDATE_CURRENT to override any notification already there
PendingIntent contentIntent = PendingIntent.getActivity(getContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = new Notification.Builder(getContext())
.setContentTitle("title")
.setContentText("content")
.setSmallIcon(R.drawable.pdficon)
.setContentIntent(contentIntent)
.setDefaults(Notification.DEFAULT_ALL)
.setPriority(Notification.PRIORITY_HIGH)
.build();
notificationManager.notify(10, notification);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
这是我在AndroidManifest.xml上的提供程序
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.com.onur.emlakdosyasi.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
Run Code Online (Sandbox Code Playgroud)
这是提供者路径:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<external-path name="Download" path="Download/"/>
Run Code Online (Sandbox Code Playgroud)
请注意,我可以手动从"下载"文件夹中打开保存的文件.我无法通过FileProvider提供的uri打开它.
将"exported"字段更改为true不会更改任何内容.
小智 8
File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Ap/" + "manual.pdf"); // -> filename = manual.pdf
Uri excelPath;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
excelPath = FileProvider.getUriForFile(mContext, "YourPackageName", pdfFile);
} else {
excelPath = Uri.fromFile(pdfFile);
}
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent.setDataAndType(excelPath, "application/pdf");
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
pdfIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
pdfIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
try{
startActivity(pdfIntent);
}catch(ActivityNotFoundException e){
Toast.makeText(mContext, "No Application available to view PDF", Toast.LENGTH_SHORT).show();
}
Run Code Online (Sandbox Code Playgroud)
小智 6
我有同样的问题,但就我而言,是这样的:
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Run Code Online (Sandbox Code Playgroud)
那不见了。
尝试了几个小时的所有方法并发布在这里作为最后的手段。15分钟后解决了..
对于任何想知道的人,我将提供者的名称属性从
android:name=".GenericFileProvider"
Run Code Online (Sandbox Code Playgroud)
到
android:name="android.support.v4.content.FileProvider"
Run Code Online (Sandbox Code Playgroud)
我什至不知道为什么我自己创建了一个文件提供程序类,但我记得遵循了它的教程。
| 归档时间: |
|
| 查看次数: |
4825 次 |
| 最近记录: |