-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
MediaLibraryUtils.java
464 lines (415 loc) · 17.9 KB
/
MediaLibraryUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
package expo.modules.medialibrary;
import android.content.Context;
import android.content.ContentResolver;
import android.content.res.AssetFileDescriptor;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.media.MediaMetadataRetriever;
import android.provider.MediaStore;
import android.provider.MediaStore.Files;
import android.provider.MediaStore.Images.Media;
import androidx.exifinterface.media.ExifInterface;
import android.text.TextUtils;
import android.net.Uri;
import android.util.Log;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.unimodules.core.Promise;
import static expo.modules.medialibrary.MediaLibraryConstants.ASSET_PROJECTION;
import static expo.modules.medialibrary.MediaLibraryConstants.ERROR_IO_EXCEPTION;
import static expo.modules.medialibrary.MediaLibraryConstants.ERROR_NO_ASSET;
import static expo.modules.medialibrary.MediaLibraryConstants.ERROR_UNABLE_TO_DELETE;
import static expo.modules.medialibrary.MediaLibraryConstants.ERROR_UNABLE_TO_LOAD;
import static expo.modules.medialibrary.MediaLibraryConstants.ERROR_UNABLE_TO_LOAD_PERMISSION;
import static expo.modules.medialibrary.MediaLibraryConstants.ERROR_UNABLE_TO_SAVE_PERMISSION;
import static expo.modules.medialibrary.MediaLibraryConstants.EXTERNAL_CONTENT;
import static expo.modules.medialibrary.MediaLibraryConstants.MEDIA_TYPES;
import static expo.modules.medialibrary.MediaLibraryConstants.MEDIA_TYPE_AUDIO;
import static expo.modules.medialibrary.MediaLibraryConstants.MEDIA_TYPE_PHOTO;
import static expo.modules.medialibrary.MediaLibraryConstants.MEDIA_TYPE_UNKNOWN;
import static expo.modules.medialibrary.MediaLibraryConstants.MEDIA_TYPE_VIDEO;
import static expo.modules.medialibrary.MediaLibraryConstants.SORT_KEYS;
import static expo.modules.medialibrary.MediaLibraryConstants.exifTags;
final class MediaLibraryUtils {
static final FileStrategy copyStrategy = new FileStrategy() {
@Override
public File apply(File src, File dir, Context context) throws IOException {
return safeCopyFile(src, dir);
}
};
static final FileStrategy moveStrategy = new FileStrategy() {
@Override
public File apply(File src, File dir, Context context) throws IOException {
File newFile = safeMoveFile(src, dir);
context.getContentResolver().delete(
EXTERNAL_CONTENT,
Media.DATA + "=?",
new String[]{src.getPath()});
return newFile;
}
};
static String[] getFileNameAndExtension(String name) {
int dot = name.lastIndexOf(".");
dot = dot != -1 ? dot : name.length();
String extension = name.substring(dot);
String filename = name.substring(0, dot);
return new String[]{filename, extension};
}
static File safeMoveFile(final File src, final File dir) throws IOException {
File copy = safeCopyFile(src, dir);
src.delete();
return copy;
}
static File safeCopyFile(final File src, final File dir) throws IOException {
File newFile = new File(dir, src.getName());
int suffix = 0;
final String[] origName = getFileNameAndExtension(src.getName());
final int suffixLimit = Short.MAX_VALUE;
while (newFile.exists()) {
newFile = new File(dir, origName[0] + "_" + suffix + origName[1]);
suffix++;
if (suffix > suffixLimit) {
throw new IOException("File name suffix limit reached (" + suffixLimit + ")");
}
}
try (FileChannel in = new FileInputStream(src).getChannel();
FileChannel out = new FileOutputStream(newFile).getChannel()) {
final long transferred = in.transferTo(0, in.size(), out);
if (transferred != in.size()) {
newFile.delete();
throw new IOException("Could not save file to " + dir + " Not enough space.");
}
return newFile;
}
}
static void queryAssetInfo(Context context, final String selection, final String[] selectionArgs, boolean fullInfo, Promise promise) {
ContentResolver contentResolver = context.getContentResolver();
try (Cursor asset = contentResolver.query(
EXTERNAL_CONTENT,
ASSET_PROJECTION,
selection,
selectionArgs,
null
)) {
if (asset == null) {
promise.reject(ERROR_UNABLE_TO_LOAD, "Could not get asset. Query returns null.");
} else {
if (asset.getCount() == 1) {
asset.moveToFirst();
ArrayList<Bundle> array = new ArrayList<>();
putAssetsInfo(contentResolver, asset, array, 1, 0, fullInfo);
// actually we want to return just the first item, but array.getMap returns ReadableMap
// which is not compatible with promise.resolve and there is no simple solution to convert
// ReadableMap to WritableMap so it's easier to return an array and pick the first item on JS side
promise.resolve(array);
} else {
promise.resolve(null);
}
}
} catch (SecurityException e) {
promise.reject(ERROR_UNABLE_TO_LOAD_PERMISSION,
"Could not get asset: need READ_EXTERNAL_STORAGE permission.", e);
} catch (IOException e) {
promise.reject(ERROR_IO_EXCEPTION, "Could not read file or parse EXIF tags", e);
}
}
static void putAssetsInfo(ContentResolver contentResolver, Cursor cursor, ArrayList<Bundle> response, int limit, int offset, boolean fullInfo) throws IOException {
final int idIndex = cursor.getColumnIndex(Media._ID);
final int filenameIndex = cursor.getColumnIndex(Media.DISPLAY_NAME);
final int mediaTypeIndex = cursor.getColumnIndex(Files.FileColumns.MEDIA_TYPE);
final int latitudeIndex = cursor.getColumnIndex(Media.LATITUDE);
final int longitudeIndex = cursor.getColumnIndex(Media.LONGITUDE);
final int creationDateIndex = cursor.getColumnIndex(Media.DATE_TAKEN);
final int modificationDateIndex = cursor.getColumnIndex(Media.DATE_MODIFIED);
final int durationIndex = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DURATION);
final int localUriIndex = cursor.getColumnIndex(Media.DATA);
final int albumIdIndex = cursor.getColumnIndex(Media.BUCKET_ID);
if (!cursor.moveToPosition(offset)) {
return;
}
for (int i = 0; i < limit && !cursor.isAfterLast(); i++) {
String path = cursor.getString(localUriIndex);
String localUri = "file://" + path;
int mediaType = cursor.getInt(mediaTypeIndex);
ExifInterface exifInterface = null;
if (mediaType == Files.FileColumns.MEDIA_TYPE_IMAGE) {
exifInterface = new ExifInterface(path);
}
int[] size = getSizeFromCursor(contentResolver, exifInterface, cursor, mediaType, localUriIndex);
Bundle asset = new Bundle();
asset.putString("id", cursor.getString(idIndex));
asset.putString("filename", cursor.getString(filenameIndex));
asset.putString("uri", localUri);
asset.putString("mediaType", exportMediaType(mediaType));
asset.putLong("width", size[0]);
asset.putLong("height", size[1]);
asset.putLong("creationTime", cursor.getLong(creationDateIndex));
asset.putDouble("modificationTime", cursor.getLong(modificationDateIndex) * 1000d);
asset.putDouble("duration", cursor.getInt(durationIndex) / 1000d);
asset.putString("albumId", cursor.getString(albumIdIndex));
if (fullInfo) {
if (exifInterface != null) {
getExifFullInfo(exifInterface, asset);
}
asset.putString("localUri", localUri);
double latitude = cursor.getDouble(latitudeIndex);
double longitude = cursor.getDouble(longitudeIndex);
// we want location to be null if it's not available
if (latitude != 0.0 || longitude != 0.0) {
Bundle location = new Bundle();
location.putDouble("latitude", latitude);
location.putDouble("longitude", longitude);
asset.putParcelable("location", location);
} else {
asset.putParcelable("location", null);
}
}
cursor.moveToNext();
response.add(asset);
}
}
static String convertSortByKey(String key) throws IllegalArgumentException {
if (!SORT_KEYS.containsKey(key)) {
String errorMessage = String.format("SortBy key \"%s\" is not supported!", key);
throw new IllegalArgumentException(errorMessage);
}
return SORT_KEYS.get(key);
}
static String exportMediaType(int mediaType) {
switch (mediaType) {
case Files.FileColumns.MEDIA_TYPE_IMAGE:
return MEDIA_TYPE_PHOTO;
case Files.FileColumns.MEDIA_TYPE_AUDIO:
case Files.FileColumns.MEDIA_TYPE_PLAYLIST:
return MEDIA_TYPE_AUDIO;
case Files.FileColumns.MEDIA_TYPE_VIDEO:
return MEDIA_TYPE_VIDEO;
default:
return MEDIA_TYPE_UNKNOWN;
}
}
static Integer convertMediaType(String mediaType) throws IllegalArgumentException {
if (!MEDIA_TYPES.containsKey(mediaType)) {
String errorMessage = String.format("MediaType \"%s\" is not supported!", mediaType);
throw new IllegalArgumentException(errorMessage);
}
return MEDIA_TYPES.get(mediaType);
}
static int[] getSizeFromCursor(ContentResolver contentResolver, ExifInterface exifInterface, Cursor cursor, int mediaType, int localUriIndex) throws IOException {
final String uri = cursor.getString(localUriIndex);
if (mediaType == Files.FileColumns.MEDIA_TYPE_VIDEO) {
Uri videoUri = Uri.parse("file://" + uri);
MediaMetadataRetriever retriever = null;
try (AssetFileDescriptor photoDescriptor = contentResolver.openAssetFileDescriptor(videoUri, "r")) {
retriever = new MediaMetadataRetriever();
retriever.setDataSource(photoDescriptor.getFileDescriptor());
int videoWidth = Integer.parseInt(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH)
);
int videoHeight = Integer.parseInt(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT)
);
int videoOrientation = Integer.parseInt(
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_ROTATION)
);
return maybeRotateAssetSize(videoWidth, videoHeight, videoOrientation);
} catch (NumberFormatException e) {
Log.e("expo-media-library", "MediaMetadataRetriever unexpectedly returned non-integer: " + e.getMessage());
} catch (FileNotFoundException e) {
Log.e("expo-media-library", String.format("ContentResolver failed to read %s: %s", uri, e.getMessage()));
} finally {
if (retriever != null) {
retriever.release();
}
}
}
final int widthIndex = cursor.getColumnIndex(MediaStore.MediaColumns.WIDTH);
final int heightIndex = cursor.getColumnIndex(MediaStore.MediaColumns.HEIGHT);
final int orientationIndex = cursor.getColumnIndex(Media.ORIENTATION);
int width = cursor.getInt(widthIndex);
int height = cursor.getInt(heightIndex);
int orientation = cursor.getInt(orientationIndex);
// If the image doesn't have the required information, we can get them from Bitmap.Options
if (mediaType == Files.FileColumns.MEDIA_TYPE_IMAGE && (width <= 0 || height <= 0)) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(uri, options);
width = options.outWidth;
height = options.outHeight;
}
if (exifInterface != null) {
int exifOrientation = exifInterface.getAttributeInt(
ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_NORMAL
);
if (
exifOrientation == ExifInterface.ORIENTATION_ROTATE_90 ||
exifOrientation == ExifInterface.ORIENTATION_ROTATE_270 ||
exifOrientation == ExifInterface.ORIENTATION_TRANSPOSE ||
exifOrientation == ExifInterface.ORIENTATION_TRANSVERSE
) {
orientation = 90;
}
}
return maybeRotateAssetSize(width, height, orientation);
}
static int[] maybeRotateAssetSize(int width, int height, int orientation) {
// given width and height might need to be swapped if the orientation is -90 or 90
if (Math.abs(orientation) % 180 == 90) {
return new int[]{height, width};
} else {
return new int[]{width, height};
}
}
static String mapOrderDescriptor(List orderDescriptor) throws IllegalArgumentException {
List<String> result = new ArrayList<>(20);
for (Object item : orderDescriptor) {
if (item instanceof String) {
String key = convertSortByKey((String) item);
result.add(key + " DESC");
} else if (item instanceof ArrayList) {
ArrayList array = (ArrayList) item;
if (array.size() != 2) {
throw new IllegalArgumentException("Array sortBy in assetsOptions has invalid layout.");
}
String key = convertSortByKey((String) array.get(0));
boolean order = (boolean) array.get(1);
result.add(key + (order ? " ASC" : " DESC"));
} else {
throw new IllegalArgumentException("Array sortBy in assetsOptions contains invalid items.");
}
}
return TextUtils.join(",", result);
}
static void getExifFullInfo(ExifInterface exifInterface, Bundle response) {
Bundle exifMap = new Bundle();
for (String[] tagInfo : exifTags) {
String name = tagInfo[1];
if (exifInterface.getAttribute(name) != null) {
String type = tagInfo[0];
switch (type) {
case "string":
exifMap.putString(name, exifInterface.getAttribute(name));
break;
case "int":
exifMap.putInt(name, exifInterface.getAttributeInt(name, 0));
break;
case "double":
exifMap.putDouble(name, exifInterface.getAttributeDouble(name, 0));
break;
}
}
}
response.putParcelable("exif", exifMap);
}
static void queryAlbum(Context context, final String selection, final String[] selectionArgs, Promise promise) {
Bundle result = new Bundle();
final String countColumn = "COUNT(*)";
final String[] projection = {Media.BUCKET_ID, Media.BUCKET_DISPLAY_NAME, countColumn};
final String selectionWithGroupBy = selection + ") GROUP BY (" + Media.BUCKET_ID;
final String group = Media.BUCKET_DISPLAY_NAME;
try (Cursor albums = context.getContentResolver().query(
EXTERNAL_CONTENT,
projection,
selectionWithGroupBy,
selectionArgs,
group)) {
if (albums == null) {
promise.reject(ERROR_UNABLE_TO_LOAD, "Could not get album. Query is incorrect.");
return;
}
if (!albums.moveToNext()) {
promise.resolve(null);
return;
}
final int bucketIdIndex = albums.getColumnIndex(Media.BUCKET_ID);
final int bucketDisplayNameIndex = albums.getColumnIndex(Media.BUCKET_DISPLAY_NAME);
final int numOfItemsIndex = albums.getColumnIndex(countColumn);
result.putString("id", albums.getString(bucketIdIndex));
result.putString("title", albums.getString(bucketDisplayNameIndex));
result.putInt("assetCount", albums.getInt(numOfItemsIndex));
promise.resolve(result);
} catch (SecurityException e) {
promise.reject(ERROR_UNABLE_TO_LOAD_PERMISSION,
"Could not get albums: need READ_EXTERNAL_STORAGE permission.", e);
}
}
static void deleteAssets(Context context, String selection, String[] selectionArgs, Promise promise) {
final String[] projection = {Media.DATA};
try (Cursor filesToDelete = context.getContentResolver().query(
EXTERNAL_CONTENT,
projection,
selection,
selectionArgs,
null)) {
if (filesToDelete == null) {
promise.reject(ERROR_UNABLE_TO_LOAD, "Could not get album. Query returns null.");
} else {
while (filesToDelete.moveToNext()) {
String filePath = filesToDelete.getString(filesToDelete.getColumnIndex(Media.DATA));
File file = new File(filePath);
if (file.delete()) {
context.getContentResolver().delete(
EXTERNAL_CONTENT,
Media.DATA + " = \"" + filePath + "\"",
null);
} else {
promise.reject(ERROR_UNABLE_TO_DELETE, "Could not delete file.");
return;
}
}
promise.resolve(true);
}
} catch (SecurityException e) {
promise.reject(ERROR_UNABLE_TO_SAVE_PERMISSION,
"Could not delete asset: need WRITE_EXTERNAL_STORAGE permission.", e);
}
}
static String getInPart(String assetsId[]) {
int length = assetsId.length;
String array[] = new String[length];
Arrays.fill(array, "?");
return TextUtils.join(",", array);
}
static List<File> getAssetsById(Context context, Promise promise, String... assetsId) {
final String[] path = {Media.DATA};
final String selection = MediaStore.Images.Media._ID + " IN ( " + getInPart(assetsId) + " )";
try (Cursor assets = context.getContentResolver().query(
EXTERNAL_CONTENT,
path,
selection,
assetsId,
null
)) {
if (assets == null) {
promise.reject(ERROR_UNABLE_TO_LOAD, "Could not get assets. Query returns null.");
return null;
} else if (assets.getCount() != assetsId.length) {
promise.reject(ERROR_NO_ASSET, "Could not get all of the requested assets");
return null;
}
List<File> assetFiles = new ArrayList<>();
while (assets.moveToNext()) {
final String assetPath = assets.getString(assets.getColumnIndex(MediaStore.Images.Media.DATA));
File asset = new File(assetPath);
if (!asset.exists() || !asset.isFile()) {
promise.reject(ERROR_UNABLE_TO_LOAD, "Path " + assetPath + " does not exist or isn't file.");
return null;
}
assetFiles.add(asset);
}
return assetFiles;
}
}
interface FileStrategy {
File apply(final File src, final File dir, Context context) throws IOException;
}
}