|
| 1 | +package com.ReactNativeBlobUtil; |
| 2 | + |
| 3 | +import android.content.ContentResolver; |
| 4 | +import android.content.ContentValues; |
| 5 | +import android.content.Context; |
| 6 | +import android.net.Uri; |
| 7 | +import android.os.Build; |
| 8 | +import android.os.Environment; |
| 9 | +import android.provider.MediaStore; |
| 10 | + |
| 11 | +import com.ReactNativeBlobUtil.Utils.FileDescription; |
| 12 | + |
| 13 | +import java.io.File; |
| 14 | + |
| 15 | +public class ReactNativeBlobUtilMediaCollection { |
| 16 | + |
| 17 | + public enum MediaType { |
| 18 | + Audio, |
| 19 | + Image, |
| 20 | + Video, |
| 21 | + Download |
| 22 | + } |
| 23 | + |
| 24 | + private Uri getMediaUri(MediaType mt) { |
| 25 | + Uri res = null; |
| 26 | + if (mt == MediaType.Audio) { |
| 27 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 28 | + res = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); |
| 29 | + } else { |
| 30 | + res = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; |
| 31 | + } |
| 32 | + } else if (mt == MediaType.Video) { |
| 33 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 34 | + res = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); |
| 35 | + } else { |
| 36 | + res = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; |
| 37 | + } |
| 38 | + } else if (mt == MediaType.Image) { |
| 39 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 40 | + res = MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); |
| 41 | + } else { |
| 42 | + res = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; |
| 43 | + } |
| 44 | + } else if (mt == MediaType.Download) { |
| 45 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 46 | + res = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return res; |
| 51 | + } |
| 52 | + |
| 53 | + private String getRelativePath(MediaType mt) { |
| 54 | + if (mt == MediaType.Audio) return Environment.DIRECTORY_MUSIC; |
| 55 | + if (mt == MediaType.Video) return Environment.DIRECTORY_MOVIES; |
| 56 | + if (mt == MediaType.Image) return Environment.DIRECTORY_PICTURES; |
| 57 | + if (mt == MediaType.Download) return Environment.DIRECTORY_DOWNLOADS; |
| 58 | + return Environment.DIRECTORY_DOWNLOADS; |
| 59 | + } |
| 60 | + |
| 61 | + public Uri createNewMediaFile(FileDescription file, MediaType mt) { |
| 62 | + // Add a specific media item. |
| 63 | + Context appCtx = ReactNativeBlobUtil.RCTContext.getApplicationContext(); |
| 64 | + ContentResolver resolver = appCtx.getContentResolver(); |
| 65 | + |
| 66 | + ContentValues fileDetails = new ContentValues(); |
| 67 | + String relativePath = getRelativePath(mt); |
| 68 | + String mimeType = getRelativePath(mt); |
| 69 | + |
| 70 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) { |
| 71 | + fileDetails.put(MediaStore.MediaColumns.DATE_ADDED, System.currentTimeMillis() / 1000); |
| 72 | + fileDetails.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000); |
| 73 | + fileDetails.put(MediaStore.MediaColumns.MIME_TYPE, mimeType); |
| 74 | + fileDetails.put(MediaStore.MediaColumns.DISPLAY_NAME, file.name); |
| 75 | + fileDetails.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath + '/' + file.partentFolder); |
| 76 | + |
| 77 | + Uri mediauri = getMediaUri(mt); |
| 78 | + |
| 79 | + // Keeps a handle to the new song's URI in case we need to modify it later. |
| 80 | + return resolver.insert(mediauri, fileDetails); |
| 81 | + } |
| 82 | + else{ |
| 83 | + File directory = Environment.getExternalStoragePublicDirectory(relativePath); |
| 84 | + if(directory.canWrite()){ |
| 85 | + File f = new File(relativePath + '/' + file.getFullPath()); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | +} |
0 commit comments