简体   繁体   中英

Android Q download PDF and open

I can not open a pdf file on andorid QI do this :

 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

This is working for me. Tested on Android R (11). Create and save a PDF in the Downloads folder on Android Q (10) & R. I use this library to generate PDFs:

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

Create the PDF and call the writeFileToDownloads method in the OnSuccess callback. 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. This will check the that the Android version is greater than or equal to 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();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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