问题描述
在我的应用程序中,按钮事件从服务器返回 PDF 的 base64 字符串。然后我将此 base64 写入文件。并尝试通过外部 PDF 查看器打开它。它在 Android 11 以外的 Android 设备上运行良好。请参阅下面的代码。
byte[] decodedContent = android.util.Base64.decode(base64.getBytes(),Base64.DEFAULT);
try {
File path = new File(getApplicationContext().getExternalFilesDir(null).getAbsolutePath(),"PDFData");
if (!path.exists()) {
path.mkdirs();
}
file = new File(path,"myPDF.pdf");
outputStream = new FileOutputStream(file);
outputStream.write(decodedContent);
outputStream.close();
Uri targetUri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(targetUri,"application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PackageManager pm = getApplicationContext().getPackageManager();
if (pm.resolveActivity(intent,PackageManager.MATCH_DEFAULT_ONLY) == null) {
Toast.makeText(getApplicationContext(),"No application found to open PDF,please install one.",Toast.LENGTH_SHORT).show();
}
else{
startActivity(intent);
}
} catch (FileNotFoundException ex) {
ex.printstacktrace();
Log.d("FileNotFound","FileNotFoundException");
} catch (IOException ex) {
ex.printstacktrace();
Log.d("IOException","IOException");
} finally {
// Make absolutely certain the outputStream is closed
try {
Log.d("outputStream","outputStream");
if (outputStream != null) {
outputStream.close();
}
} catch (IOException ex) {
ex.printstacktrace();
}
清单文件中已包含以下权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
请帮我解决这个问题。提前致谢!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)