如何在 Android 10 及以上版本中使用 JAVA 将 PDF 文件保存到媒体存储中?

huangapple go评论149阅读模式
英文:

How to save pdf file in a Media Store in Android 10 and above using JAVA?

问题

更新:

为了保存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) {
            // 不要在MediaStore中留下孤立的条目
            resolver.delete(uri, null, null);
        }

        throw e;
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
英文:

UPDATE:

In order to save PDF file:

in the answer section below.

In order to save bitmap file:

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

答案1

得分: 12

@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();
        }
    }
}
英文:

Save PDF file in android 10 and above:

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

huangapple
  • 本文由 发表于 2020年8月19日 12:53:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63480192.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定