如何使用JAVA将pdf文件保存在Android 10及更高版本的Media Store中? [英] How to save pdf file in a Media Store in Android 10 and above using JAVA?

查看:130
本文介绍了如何使用JAVA将pdf文件保存在Android 10及更高版本的Media Store中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:

要保存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();
        }
    }
}

推荐答案

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

这篇关于如何使用JAVA将pdf文件保存在Android 10及更高版本的Media Store中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆