简体   繁体   English

Android Q 下载PDF并打开

[英]Android Q download PDF and open

I can not open a pdf file on andorid QI do this :我无法在 andorid QI 上打开 pdf 文件,请执行以下操作:

 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);
        }

I see a black screen and in logs I have :我看到一个黑屏,在日志中我有:

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 );
    }

This is how I open pdf file on android QI see a black file , a file is on folder Download这就是我在 android 上打开 pdf 文件的方式 QI 看到一个黑色文件,一个文件在文件夹中 下载

This is working for me.这对我有用。 Tested on Android R (11).在 Android R (11) 上测试。 Create and save a PDF in the Downloads folder on Android Q (10) & R. I use this library to generate PDFs:在 Android Q (10) & R 上的 Downloads 文件夹中创建并保存 PDF。我使用这个库来生成 PDF:

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

Create the PDF and call the writeFileToDownloads method in the OnSuccess callback.创建 PDF 并在 OnSuccess 回调中调用 writeFileToDownloads 方法。 As a bonus, a download notification is created:作为奖励,创建了一个下载通知:

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();

To save the PDF in the downloads folder, call the writeFileToDownloads method.要将 PDF 保存在下载文件夹中,请调用 writeFileToDownloads 方法。 This will check the that the Android version is greater than or equal to Q:这将检查 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();
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM