Android Q 下载 PDF 并打开



我无法在 andorid 上打开 pdf 文件 Q 我这样做:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
OutputStream fosTemp;
ContentResolver resolver = context.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Files.FileColumns.DISPLAY_NAME, "mamy123");
contentValues.put(MediaStore.Files.FileColumns.MIME_TYPE, "application/pdf");
contentValues.put(MediaStore.Files.FileColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
Uri imageUri2 = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
fosTemp = resolver.openOutputStream(imageUri2);
byte[] pdfAsBytes = Base64.decode(base64, 0);
fosTemp.write(pdfAsBytes);
fosTemp.flush();
fosTemp.close();
openPDF(context, imageUri2);
}

我看到一个黑屏,在日志中我有:

java.lang.SecurityException: com.google.android.apps.docs has no access to content://media/external/downloads/49

public static void openPDF(Context context, Uri localUri) {
Intent i = new Intent( Intent.ACTION_VIEW );
i.setDataAndType( localUri, PDF_MIME_TYPE );
context.startActivity( i );
}

这就是我在android Q上打开pdf文件的方式,我看到一个黑色文件,一个文件在文件夹中下载

这对我有用。在安卓R(11(上测试。在Android Q (10( & R的"下载"文件夹中创建并保存PDF。我使用此库生成 PDF:

implementation 'com.uttampanchasara.pdfgenerator:pdfgenerator:1.3'

创建 PDF 并在 OnSuccess 回调中调用 writeFileToDownloads 方法。作为奖励,将创建下载通知:

String root = mContext.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS).toString();
File file = new File(root, documentTitle + ".pdf");
if (root != null) {
new CreatePdf(mContext)
.setPdfName(documentTitle)
.openPrintDialog(false)
.setContentBaseUrl(null)
.setPageSize(PrintAttributes.MediaSize.ISO_A4)
.setContent(documentContent)
.setFilePath(root)
.setCallbackListener(new CreatePdf.PdfCallbackListener() {
@Override
public void onFailure(String s) {
Toast.makeText(mContext, mContext.getResources().getString(R.string.toast_an_error_occurred), Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(String s) {
DownloadManager downloadManager = (DownloadManager) mContext.getSystemService(DOWNLOAD_SERVICE);
downloadManager.addCompletedDownload(file.getName(), file.getName(), true, "application/pdf", file.getAbsolutePath(), file.length(), true);
writeFileToDownloads(file);                         
}
})
.create();

若要将 PDF 保存在下载文件夹中,请调用 writeFileToDownloads 方法。这将检查安卓版本是否大于或等于 Q:

private void writeFileToDownloads(File file) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentResolver resolver = mContext.getContentResolver();
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, file.getName());
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "application/pdf");
contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS);
Uri uri = resolver.insert(MediaStore.Downloads.EXTERNAL_CONTENT_URI, contentValues);
try {
int size = (int) file.length();
byte[] bytes = new byte[size];
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
OutputStream fos = resolver.openOutputStream(uri);
fos.write(bytes);
fos.flush();
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

最新更新