MediaStoreHelper.java 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package com.datarecovery.master.utils;
  2. import android.content.ContentResolver;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.net.Uri;
  6. import android.os.Build;
  7. import android.os.ParcelFileDescriptor;
  8. import android.provider.MediaStore;
  9. import androidx.annotation.IntDef;
  10. import androidx.annotation.RequiresApi;
  11. import com.atmob.common.runtime.ContextUtil;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileOutputStream;
  15. import java.io.InputStream;
  16. import java.lang.annotation.Retention;
  17. import java.lang.annotation.RetentionPolicy;
  18. /**
  19. * 用于保存文件到公共存储空间
  20. */
  21. public class MediaStoreHelper {
  22. public static final int TYPE_IMAGE = 1;
  23. public static final int TYPE_VIDEO = 2;
  24. public static final int TYPE_AUDIO = 3;
  25. public static final int TYPE_FILES = 4;
  26. public static final int TYPE_DOWNLOADS = 5;
  27. @Retention(RetentionPolicy.SOURCE)
  28. @IntDef({TYPE_IMAGE, TYPE_VIDEO, TYPE_AUDIO, TYPE_FILES, TYPE_DOWNLOADS})
  29. @interface MediaType {
  30. }
  31. public static void saveToSharedStorage(@MediaType int mediaType, File file, String fileName) throws Exception {
  32. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
  33. saveToSharedStorage(mediaType, java.nio.file.Files.newInputStream(file.toPath()), fileName);
  34. } else {
  35. saveToSharedStorage(mediaType, new FileInputStream(file), fileName);
  36. }
  37. }
  38. public static void saveToSharedStorage(@MediaType int mediaType, InputStream inputStream, String fileName) throws Exception {
  39. // Add a media item that other apps don't see until the item is
  40. // fully written to the media store.
  41. Context applicationContext = ContextUtil.getContext().getApplicationContext();
  42. ContentResolver resolver = applicationContext.getContentResolver();
  43. Media targetMedia = getTargetMedia(mediaType);
  44. // Find all media files on the primary external storage device.
  45. Uri mediaCollection = targetMedia.getMediaCollection();
  46. ContentValues mediaDetails = new ContentValues();
  47. mediaDetails.put(targetMedia.displayName(), fileName);
  48. // lock media file until it's fully written.
  49. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  50. mediaDetails.put(targetMedia.isPending(), 1);
  51. }
  52. Uri mediaContentUri = resolver.insert(mediaCollection, mediaDetails);
  53. if (mediaContentUri == null) {
  54. throw new IllegalStateException("Failed to create new media item.");
  55. }
  56. // "w" for write.
  57. try (ParcelFileDescriptor pfd =
  58. resolver.openFileDescriptor(mediaContentUri, "w", null);
  59. InputStream is = inputStream;
  60. FileOutputStream fos = pfd == null ? null : new FileOutputStream(pfd.getFileDescriptor())
  61. ) {
  62. if (fos == null) {
  63. throw new IllegalStateException("Failed to open new media item.");
  64. }
  65. // Write data into the pending media file.
  66. byte[] buf = new byte[8192];
  67. int len;
  68. while ((len = is.read(buf)) > 0) {
  69. fos.write(buf, 0, len);
  70. }
  71. fos.flush();
  72. } finally {
  73. // finished, release the "pending" status.
  74. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  75. mediaDetails.clear();
  76. mediaDetails.put(targetMedia.isPending(), 0);
  77. resolver.update(mediaContentUri, mediaDetails, null, null);
  78. }
  79. }
  80. }
  81. private static Media getTargetMedia(int mediaType) {
  82. switch (mediaType) {
  83. case TYPE_IMAGE:
  84. return new Image();
  85. case TYPE_VIDEO:
  86. return new Video();
  87. case TYPE_AUDIO:
  88. return new Audio();
  89. case TYPE_FILES:
  90. return new Files();
  91. case TYPE_DOWNLOADS:
  92. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  93. return new Downloads();
  94. }
  95. return new Files();
  96. default:
  97. throw new IllegalArgumentException("Unknown media type: " + mediaType);
  98. }
  99. }
  100. private static class Image implements Media {
  101. @Override
  102. public Uri getMediaCollection() {
  103. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  104. return MediaStore.Images.Media
  105. .getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
  106. } else {
  107. return MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  108. }
  109. }
  110. @Override
  111. public String displayName() {
  112. return MediaStore.Images.Media.DISPLAY_NAME;
  113. }
  114. @Override
  115. public String isPending() {
  116. return MediaStore.Images.Media.IS_PENDING;
  117. }
  118. }
  119. private static class Video implements Media {
  120. @Override
  121. public Uri getMediaCollection() {
  122. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  123. return MediaStore.Video.Media
  124. .getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
  125. } else {
  126. return MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
  127. }
  128. }
  129. @Override
  130. public String displayName() {
  131. return MediaStore.Video.Media.DISPLAY_NAME;
  132. }
  133. @Override
  134. public String isPending() {
  135. return MediaStore.Video.Media.IS_PENDING;
  136. }
  137. }
  138. private static class Audio implements Media {
  139. @Override
  140. public Uri getMediaCollection() {
  141. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  142. return MediaStore.Audio.Media
  143. .getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
  144. } else {
  145. return MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  146. }
  147. }
  148. @Override
  149. public String displayName() {
  150. return MediaStore.Audio.Media.DISPLAY_NAME;
  151. }
  152. @Override
  153. public String isPending() {
  154. return MediaStore.Audio.Media.IS_PENDING;
  155. }
  156. }
  157. private static class Files implements Media {
  158. @Override
  159. public Uri getMediaCollection() {
  160. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  161. return MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
  162. } else {
  163. return MediaStore.Files.getContentUri("external");
  164. }
  165. }
  166. @Override
  167. public String displayName() {
  168. return MediaStore.Files.FileColumns.DISPLAY_NAME;
  169. }
  170. @Override
  171. public String isPending() {
  172. return MediaStore.Files.FileColumns.IS_PENDING;
  173. }
  174. }
  175. private static class Downloads implements Media {
  176. @RequiresApi(api = Build.VERSION_CODES.Q)
  177. @Override
  178. public Uri getMediaCollection() {
  179. return MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
  180. }
  181. @Override
  182. public String displayName() {
  183. return MediaStore.Downloads.DISPLAY_NAME;
  184. }
  185. @Override
  186. public String isPending() {
  187. return MediaStore.Downloads.IS_PENDING;
  188. }
  189. }
  190. private interface Media {
  191. Uri getMediaCollection();
  192. String displayName();
  193. String isPending();
  194. }
  195. }