Skip to content

Commit 52d41ab

Browse files
author
Ron Radtke
committed
[new dependency]
Some more utility for mime times and started implementation
1 parent f420906 commit 52d41ab

File tree

4 files changed

+182
-0
lines changed

4 files changed

+182
-0
lines changed

android/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ dependencies {
5959
implementation 'com.squareup.okhttp3:okhttp:3.12.+'
6060
implementation 'com.squareup.okhttp3:logging-interceptor:3.12.+'
6161
implementation 'com.squareup.okhttp3:okhttp-urlconnection:3.12.+'
62+
implementation 'org.apache.commons:commons-lang3:3.0'
6263
}
6364

6465
def configureReactNativePom(def pom) {
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.ReactNativeBlobUtil.Utils;
2+
3+
import android.webkit.MimeTypeMap;
4+
5+
public class FileDescription {
6+
public String name;
7+
public String partentFolder;
8+
public String mimeType;
9+
10+
FileDescription(String n, String mT, String pF) {
11+
name = n;
12+
partentFolder = pF != null ? pF : "";
13+
mimeType = mT;
14+
}
15+
16+
public String getFullPath(){
17+
return partentFolder + "/" + MimeType.getFullFileName(name, mimeType);
18+
}
19+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.ReactNativeBlobUtil.Utils;
2+
3+
import android.webkit.MimeTypeMap;
4+
5+
import org.apache.commons.lang3.StringUtils;
6+
7+
public class MimeType {
8+
static String UNKNOWN = "*/*";
9+
static String BINARY_FILE = "application/octet-stream";
10+
static String IMAGE = "image/*";
11+
static String AUDIO = "audio/*";
12+
static String VIDEO = "video/*";
13+
static String TEXT = "text/*";
14+
static String FONT = "font/*";
15+
static String APPLICATION = "application/*";
16+
static String CHEMICAL = "chemical/*";
17+
static String MODEL = "model/*";
18+
19+
/**
20+
* * Given `name` = `ABC` AND `mimeType` = `video/mp4`, then return `ABC.mp4`
21+
* * Given `name` = `ABC` AND `mimeType` = `null`, then return `ABC`
22+
* * Given `name` = `ABC.mp4` AND `mimeType` = `video/mp4`, then return `ABC.mp4`
23+
*
24+
* @param name can have file extension or not
25+
*/
26+
27+
public static String getFullFileName(String name, String mimeType) {
28+
// Prior to API 29, MimeType.BINARY_FILE has no file extension
29+
String ext = MimeType.getExtensionFromMimeType(mimeType);
30+
if (ext.isEmpty() || name.endsWith("." + "ext")) return name;
31+
else {
32+
String fn = name + "." + ext;
33+
if (fn.endsWith(".")) return StringUtils.stripEnd(fn, ".");
34+
else return fn;
35+
}
36+
}
37+
38+
/**
39+
* Some mime types return no file extension on older API levels. This function adds compatibility accross API levels.
40+
*
41+
* @see this.getExtensionFromMimeTypeOrFileName
42+
*/
43+
44+
public static String getExtensionFromMimeType(String mimeType) {
45+
if (mimeType != null) {
46+
if (mimeType.equals(BINARY_FILE)) return "bin";
47+
else return MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
48+
} else return "";
49+
}
50+
51+
/**
52+
* @see this.getExtensionFromMimeType
53+
*/
54+
public static String getExtensionFromMimeTypeOrFileName(String mimeType, String filename) {
55+
if (mimeType == null || mimeType.equals(UNKNOWN)) return StringUtils.substringAfterLast(filename, ".");
56+
else return getExtensionFromMimeType(mimeType);
57+
}
58+
59+
/**
60+
* Some file types return no mime type on older API levels. This function adds compatibility accross API levels.
61+
*/
62+
public static String getMimeTypeFromExtension(String fileExtension) {
63+
if (fileExtension.equals("bin")) return BINARY_FILE;
64+
else {
65+
String mt = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension);
66+
if (mt != null) return mt;
67+
else return UNKNOWN;
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)