2

更新:

为了保存 PDF 文件:

在下面的答案部分。

为了保存位图文件:

@RequiresApi(api = Build.VERSION_CODES.Q)
@NonNull
private Uri saveBitmap(@NonNull final Context context, @NonNull final Bitmap bitmap,
                       @NonNull final Bitmap.CompressFormat format, @NonNull final String mimeType,
                       @NonNull final String displayName, @Nullable final String subFolder) throws IOException {
    String relativeLocation = Environment.DIRECTORY_PICTURES;

    if (!TextUtils.isEmpty(subFolder)) {
        relativeLocation += File.separator + subFolder;
    }

    final ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);

    final ContentResolver resolver = context.getContentResolver();

    OutputStream stream = null;
    Uri uri = null;

    try {
        final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        uri = resolver.insert(contentUri, contentValues);

        if (uri == null) {
            throw new IOException("Failed to create new MediaStore record.");
        }

        stream = resolver.openOutputStream(uri);

        if (stream == null) {
            throw new IOException("Failed to get output stream.");
        }

        if (!bitmap.compress(format, 95, stream)) {
            throw new IOException("Failed to save bitmap.");
        }

        return uri;
    } catch (IOException e) {
        if (uri != null) {
            // Don't leave an orphan entry in the MediaStore
            resolver.delete(uri, null, null);
        }

        throw e;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
4

1 回答 1

9

在 android 10 及更高版本中保存 PDF 文件:

@RequiresApi(api = Build.VERSION_CODES.Q)
private void savePdfAndLogUri() {
    try {
        InputStream in = getAssets().open("eliza.pdf");
        Uri savedFileUri = savePDFFile(MainActivity.this, in, "files/pdf", "eliza.pdf", "resume");
        Log.d("URI: ", savedFileUri.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }
}


@RequiresApi(api = Build.VERSION_CODES.Q)
@NonNull
private Uri savePDFFile(@NonNull final Context context, @NonNull InputStream in,
                        @NonNull final String mimeType,
                        @NonNull final String displayName, @Nullable final String subFolder) throws IOException {
    String relativeLocation = Environment.DIRECTORY_DOCUMENTS;

    if (!TextUtils.isEmpty(subFolder)) {
        relativeLocation += File.separator + subFolder;
    }
    
    final ContentValues contentValues = new ContentValues();
    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
    contentValues.put(MediaStore.Video.Media.TITLE, "SomeName");
    contentValues.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
    contentValues.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
    final ContentResolver resolver = context.getContentResolver();
    OutputStream stream = null;
    Uri uri = null;
    
    try {
        final Uri contentUri = MediaStore.Files.getContentUri("external");
        uri = resolver.insert(contentUri, contentValues);
        ParcelFileDescriptor pfd;
        try {
            assert uri != null;
            pfd = getContentResolver().openFileDescriptor(uri, "w");
            assert pfd != null;
            FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());

            byte[] buf = new byte[4 * 1024];
            int len;
            while ((len = in.read(buf)) > 0) {

                out.write(buf, 0, len);
            }
            out.close();
            in.close();
            pfd.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        contentValues.clear();
        contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
        getContentResolver().update(uri, contentValues, null, null);
        stream = resolver.openOutputStream(uri);
        if (stream == null) {
            throw new IOException("Failed to get output stream.");
        }
        return uri;
    } catch (IOException e) {
        // Don't leave an orphan entry in the MediaStore
        resolver.delete(uri, null, null);
        throw e;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
于 2020-08-20T04:48:00.337 回答