如何使用java在android10及以上版本的媒体商店中保存pdf文件?

ukdjmx9f  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(597)

更新:
要保存pdf文件:
在下面的答案部分。
要保存位图文件:

  1. @RequiresApi(api = Build.VERSION_CODES.Q)
  2. @NonNull
  3. private Uri saveBitmap(@NonNull final Context context, @NonNull final Bitmap bitmap,
  4. @NonNull final Bitmap.CompressFormat format, @NonNull final String mimeType,
  5. @NonNull final String displayName, @Nullable final String subFolder) throws IOException {
  6. String relativeLocation = Environment.DIRECTORY_PICTURES;
  7. if (!TextUtils.isEmpty(subFolder)) {
  8. relativeLocation += File.separator + subFolder;
  9. }
  10. final ContentValues contentValues = new ContentValues();
  11. contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
  12. contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
  13. contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
  14. final ContentResolver resolver = context.getContentResolver();
  15. OutputStream stream = null;
  16. Uri uri = null;
  17. try {
  18. final Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  19. uri = resolver.insert(contentUri, contentValues);
  20. if (uri == null) {
  21. throw new IOException("Failed to create new MediaStore record.");
  22. }
  23. stream = resolver.openOutputStream(uri);
  24. if (stream == null) {
  25. throw new IOException("Failed to get output stream.");
  26. }
  27. if (!bitmap.compress(format, 95, stream)) {
  28. throw new IOException("Failed to save bitmap.");
  29. }
  30. return uri;
  31. } catch (IOException e) {
  32. if (uri != null) {
  33. // Don't leave an orphan entry in the MediaStore
  34. resolver.delete(uri, null, null);
  35. }
  36. throw e;
  37. } finally {
  38. if (stream != null) {
  39. stream.close();
  40. }
  41. }
  42. }
q5lcpyga

q5lcpyga1#

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

  1. @RequiresApi(api = Build.VERSION_CODES.Q)
  2. private void savePdfAndLogUri() {
  3. try {
  4. InputStream in = getAssets().open("eliza.pdf");
  5. Uri savedFileUri = savePDFFile(MainActivity.this, in, "files/pdf", "eliza.pdf", "resume");
  6. Log.d("URI: ", savedFileUri.toString());
  7. } catch (IOException e) {
  8. e.printStackTrace();
  9. }
  10. }
  11. @RequiresApi(api = Build.VERSION_CODES.Q)
  12. @NonNull
  13. private Uri savePDFFile(@NonNull final Context context, @NonNull InputStream in,
  14. @NonNull final String mimeType,
  15. @NonNull final String displayName, @Nullable final String subFolder) throws IOException {
  16. String relativeLocation = Environment.DIRECTORY_DOCUMENTS;
  17. if (!TextUtils.isEmpty(subFolder)) {
  18. relativeLocation += File.separator + subFolder;
  19. }
  20. final ContentValues contentValues = new ContentValues();
  21. contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, displayName);
  22. contentValues.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
  23. contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, relativeLocation);
  24. contentValues.put(MediaStore.Video.Media.TITLE, "SomeName");
  25. contentValues.put(MediaStore.Video.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
  26. contentValues.put(MediaStore.Video.Media.DATE_TAKEN, System.currentTimeMillis());
  27. final ContentResolver resolver = context.getContentResolver();
  28. OutputStream stream = null;
  29. Uri uri = null;
  30. try {
  31. final Uri contentUri = MediaStore.Files.getContentUri("external");
  32. uri = resolver.insert(contentUri, contentValues);
  33. ParcelFileDescriptor pfd;
  34. try {
  35. assert uri != null;
  36. pfd = getContentResolver().openFileDescriptor(uri, "w");
  37. assert pfd != null;
  38. FileOutputStream out = new FileOutputStream(pfd.getFileDescriptor());
  39. byte[] buf = new byte[4 * 1024];
  40. int len;
  41. while ((len = in.read(buf)) > 0) {
  42. out.write(buf, 0, len);
  43. }
  44. out.close();
  45. in.close();
  46. pfd.close();
  47. } catch (Exception e) {
  48. e.printStackTrace();
  49. }
  50. contentValues.clear();
  51. contentValues.put(MediaStore.Video.Media.IS_PENDING, 0);
  52. getContentResolver().update(uri, contentValues, null, null);
  53. stream = resolver.openOutputStream(uri);
  54. if (stream == null) {
  55. throw new IOException("Failed to get output stream.");
  56. }
  57. return uri;
  58. } catch (IOException e) {
  59. // Don't leave an orphan entry in the MediaStore
  60. resolver.delete(uri, null, null);
  61. throw e;
  62. } finally {
  63. if (stream != null) {
  64. stream.close();
  65. }
  66. }
  67. }
展开查看全部

相关问题