GalleryDataProvider.java 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. package ai.nreal.android.gallery;
  2. import android.content.ContentResolver;
  3. import android.content.ContentValues;
  4. import android.content.Context;
  5. import android.graphics.Bitmap;
  6. import android.graphics.BitmapFactory;
  7. import android.net.Uri;
  8. import android.os.Build;
  9. import android.os.Environment;
  10. import android.provider.MediaStore;
  11. import android.util.Log;
  12. import java.io.File;
  13. import java.io.FileInputStream;
  14. import java.io.FileNotFoundException;
  15. import java.io.IOException;
  16. import java.io.InputStream;
  17. import java.io.OutputStream;
  18. public class GalleryDataProvider {
  19. private final static String TAG = GalleryDataProvider.class.getSimpleName();
  20. private Context mContext;
  21. private ContentResolver mContentResolver;
  22. public GalleryDataProvider(Context context) {
  23. mContext = context;
  24. mContentResolver = mContext.getContentResolver();
  25. }
  26. //@RequiresApi(api = Build.VERSION_CODES.Q)
  27. public Uri insertImage(final Bitmap bitmap, final String displayName,
  28. final String folderName) {
  29. ContentValues values = new ContentValues();
  30. values.put(MediaStore.Images.Media.DISPLAY_NAME, displayName);
  31. values.put(MediaStore.Images.Media.MIME_TYPE, "image/png");
  32. long currentTime = System.currentTimeMillis() / 1000;
  33. values.put(MediaStore.Images.ImageColumns.DATE_ADDED, currentTime);
  34. values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, currentTime);
  35. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  36. values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, currentTime);
  37. values.put(MediaStore.MediaColumns.RELATIVE_PATH,
  38. Environment.DIRECTORY_PICTURES + File.separator + folderName);
  39. }
  40. Uri imageUri = mContentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  41. try (OutputStream outputStream = mContentResolver.openOutputStream(imageUri)) {
  42. boolean success = bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);
  43. if (!success) {
  44. Log.e(TAG, "insertImageAsPNG: bitmap compressed to output stream failed");
  45. }
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }
  49. return imageUri;
  50. }
  51. //@RequiresApi(api = Build.VERSION_CODES.Q)
  52. public Uri insertImage(final InputStream inputStream, final String displayName,
  53. final String folderName, final String mimeType) {
  54. ContentValues values = new ContentValues();
  55. values.put(MediaStore.Images.Media.DISPLAY_NAME, displayName);
  56. values.put(MediaStore.Images.Media.MIME_TYPE, mimeType);
  57. long currentTime = System.currentTimeMillis() / 1000;
  58. values.put(MediaStore.Images.ImageColumns.DATE_ADDED, currentTime);
  59. values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, currentTime);
  60. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  61. values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, currentTime);
  62. values.put(MediaStore.MediaColumns.RELATIVE_PATH,
  63. Environment.DIRECTORY_PICTURES + File.separator + folderName);
  64. }
  65. Uri imageUri = mContentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  66. if (imageUri == null) {
  67. return null;
  68. }
  69. try (OutputStream outputStream = mContentResolver.openOutputStream(imageUri)) {
  70. writeStreamToOutput(inputStream, outputStream);
  71. } catch (IOException e) {
  72. e.printStackTrace();
  73. }
  74. return imageUri;
  75. }
  76. //@RequiresApi(api = Build.VERSION_CODES.Q)
  77. public Uri insertImage(final String imagePath, final String displayName, final String folderName) {
  78. final BitmapFactory.Options options = new BitmapFactory.Options();
  79. options.inJustDecodeBounds = true;
  80. BitmapFactory.decodeFile(imagePath, options);
  81. ContentValues values = new ContentValues();
  82. values.put(MediaStore.Images.Media.DISPLAY_NAME, displayName);
  83. values.put(MediaStore.Images.Media.MIME_TYPE, options.outMimeType);
  84. long currentTime = System.currentTimeMillis() / 1000;
  85. values.put(MediaStore.Images.ImageColumns.DATE_ADDED, currentTime);
  86. values.put(MediaStore.Images.ImageColumns.DATE_MODIFIED, currentTime);
  87. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  88. values.put(MediaStore.Images.ImageColumns.DATE_TAKEN, currentTime);
  89. values.put(MediaStore.MediaColumns.RELATIVE_PATH,
  90. Environment.DIRECTORY_PICTURES + File.separator + folderName);
  91. }
  92. Uri imageUri = mContentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
  93. try {
  94. File file = new File(imagePath);
  95. FileInputStream inputStream = new FileInputStream(file);
  96. OutputStream outputStream = mContentResolver.openOutputStream(imageUri);
  97. writeStreamToOutput(inputStream, outputStream);
  98. } catch (IOException e) {
  99. e.printStackTrace();
  100. }
  101. return imageUri;
  102. }
  103. /**
  104. * @param inputStream 原始文件流
  105. * @param displayName 存入的文件名 需要后缀(.mp4)
  106. * @param folderName 存入的文件夹
  107. * @param mimeType 类型 :"video/mp4"
  108. * @return 返回的uri路径
  109. */
  110. public Uri insertVideo(final InputStream inputStream, final String displayName,
  111. final String folderName, final String mimeType) {
  112. ContentValues values = new ContentValues();
  113. values.put(MediaStore.Video.Media.DISPLAY_NAME, displayName);
  114. values.put(MediaStore.Video.Media.MIME_TYPE, mimeType);
  115. values.put(MediaStore.MediaColumns.RELATIVE_PATH,
  116. Environment.DIRECTORY_MOVIES + File.separator + folderName);
  117. Uri videoUri = mContentResolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
  118. if (videoUri == null) {
  119. return null;
  120. }
  121. try (OutputStream outputStream = mContentResolver.openOutputStream(videoUri)) {
  122. writeStreamToOutput(inputStream, outputStream);
  123. } catch (FileNotFoundException e) {
  124. e.printStackTrace();
  125. } catch (IOException e) {
  126. e.printStackTrace();
  127. }
  128. return videoUri;
  129. }
  130. /**
  131. * @param originFilePath 原始文件路径
  132. * @param displayName 存入的文件名 需要后缀(.mp4)
  133. * @param folderName 存入的文件夹
  134. * @return 返回的uri路径
  135. */
  136. public Uri insertVideo(final String originFilePath, final String displayName,
  137. final String folderName) {
  138. Uri uri = null;
  139. try {
  140. FileInputStream inputStream = new FileInputStream(new File(originFilePath));
  141. uri = insertVideo(inputStream, displayName, folderName, "video/mp4");
  142. } catch (FileNotFoundException e) {
  143. e.printStackTrace();
  144. }
  145. return uri;
  146. }
  147. // endregion
  148. private void writeStreamToOutput(InputStream inputStream, OutputStream outputStream) {
  149. try {
  150. byte[] buffer = new byte[4 * 1024]; // or other buffer size
  151. int read;
  152. while ((read = inputStream.read(buffer)) != -1) {
  153. outputStream.write(buffer, 0, read);
  154. }
  155. outputStream.flush();
  156. } catch (FileNotFoundException e) {
  157. Log.i(TAG, e.toString());
  158. e.printStackTrace();
  159. } catch (IOException e) {
  160. e.printStackTrace();
  161. }
  162. }
  163. }