Java Android Q下载PDF并打开
我无法在上打开pdf文件,请执行以下操作:Java Android Q下载PDF并打开,java,android,Java,Android,我无法在上打开pdf文件,请执行以下操作: if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { OutputStream fosTemp; ContentResolver resolver = context.getContentResolver(); ContentValues contentValues = new ContentValues();
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上打开pdf文件的方式Q我看到一个黑色文件,一个文件在文件夹下载中这对我来说很有用。在安卓R11上测试。在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方法。这将检查Android版本是否大于或等于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();
}
}
}
这段代码中没有任何东西可以下载。请告诉导致异常的代码行。logcat会告诉您。没有最后一行,您就没有异常?@blackapps here:public static void openPDFContext context,Uri localUri{Intent i=new Intent Intent Intent.ACTION\u VIEW;i.setDataAndType localUri,PDF\u MIME\u TYPE;context.startActivity i;}@blackapps现在我在文件夹中有一个文件:下载,但我无法打开此文件,我有黑屏尝试,具有I.putFlagIntent.FLAG\u授予\u读取\u URI\u权限;