Android 如何在Andriod中使用绝对路径文件共享pdf文件
如何在android中共享pdf文件我可以使用pdfView库从a解决方案路径文件查看pdf,因此我认为路径正确,但无法使用查看pdf的相同路径共享,我还尝试使用toast查看路径,该路径给出了正确的路径,但当我尝试共享文件时,应用程序崩溃了,这就是我使用的代码Android 如何在Andriod中使用绝对路径文件共享pdf文件,android,android-intent,android-sharing,Android,Android Intent,Android Sharing,如何在android中共享pdf文件我可以使用pdfView库从a解决方案路径文件查看pdf,因此我认为路径正确,但无法使用查看pdf的相同路径共享,我还尝试使用toast查看路径,该路径给出了正确的路径,但当我尝试共享文件时,应用程序崩溃了,这就是我使用的代码 Uri uri = Uri.fromFile(new File(arrayList.get(i).getAbsolutePath())); Intent intent = new Intent();
Uri uri = Uri.fromFile(new File(arrayList.get(i).getAbsolutePath()));
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
activity.startActivity(Intent.createChooser(intent, "Share Via"));
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, "No Sharing App Found", Toast.LENGTH_SHORT).show();
}
``
这就是最终有效的解决方案 首先,它进入应用程序关闭标记之前的清单
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/provider_paths"/>
</provider>
logcat中的错误是什么?Emulator不工作,因此我只能在真实设备上测试您的应用程序在出现
FileUrieExposedException
时崩溃。这应该在logcat中可见。arrayList.get(i.getAbsolutePath())
错误发布。没有人知道那将是哪条路。@blackapps谢谢你的评论实际上引导我找到了一个解决方案
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path name="external_files" path="."/>
</paths>
Uri uri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", new File(pdf_list.get(i).getAbsolutePath()));
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
intent.setAction(Intent.ACTION_SEND);
intent.setType("application/pdf");
intent.putExtra(Intent.EXTRA_SUBJECT, "");
intent.putExtra(Intent.EXTRA_TEXT, "");
intent.putExtra(Intent.EXTRA_STREAM, uri);
try {
activity.startActivity(Intent.createChooser(intent, "Share Via"));
} catch (ActivityNotFoundException e) {
Toast.makeText(activity, "No Sharing App Found", Toast.LENGTH_SHORT).show();
}