2016-01-14 04:31:51 -08:00
|
|
|
/**
|
2018-09-11 15:27:47 -07:00
|
|
|
* Copyright (c) Facebook, Inc. and its affiliates.
|
2016-01-14 04:31:51 -08:00
|
|
|
*
|
2018-02-16 18:24:55 -08:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2016-01-14 04:31:51 -08:00
|
|
|
*/
|
|
|
|
|
|
|
|
package com.facebook.react.modules.camera;
|
|
|
|
|
|
|
|
import android.content.ContentResolver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.res.AssetFileDescriptor;
|
|
|
|
import android.database.Cursor;
|
|
|
|
import android.graphics.BitmapFactory;
|
2017-08-15 11:36:42 -07:00
|
|
|
import android.media.MediaMetadataRetriever;
|
2016-01-14 04:31:51 -08:00
|
|
|
import android.media.MediaScannerConnection;
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.os.AsyncTask;
|
|
|
|
import android.os.Build;
|
|
|
|
import android.os.Environment;
|
|
|
|
import android.provider.MediaStore;
|
|
|
|
import android.provider.MediaStore.Images;
|
2019-01-28 07:31:29 -08:00
|
|
|
import android.provider.MediaStore.MediaColumns;
|
2016-01-14 04:31:51 -08:00
|
|
|
import android.text.TextUtils;
|
|
|
|
import com.facebook.common.logging.FLog;
|
|
|
|
import com.facebook.react.bridge.GuardedAsyncTask;
|
2017-08-02 19:59:54 -07:00
|
|
|
import com.facebook.react.bridge.JSApplicationIllegalArgumentException;
|
|
|
|
import com.facebook.react.bridge.NativeModule;
|
2016-01-21 08:07:01 -08:00
|
|
|
import com.facebook.react.bridge.Promise;
|
2016-01-14 04:31:51 -08:00
|
|
|
import com.facebook.react.bridge.ReactApplicationContext;
|
|
|
|
import com.facebook.react.bridge.ReactContext;
|
|
|
|
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
|
|
import com.facebook.react.bridge.ReactMethod;
|
|
|
|
import com.facebook.react.bridge.ReadableArray;
|
|
|
|
import com.facebook.react.bridge.ReadableMap;
|
|
|
|
import com.facebook.react.bridge.WritableArray;
|
|
|
|
import com.facebook.react.bridge.WritableMap;
|
|
|
|
import com.facebook.react.bridge.WritableNativeArray;
|
|
|
|
import com.facebook.react.bridge.WritableNativeMap;
|
|
|
|
import com.facebook.react.common.ReactConstants;
|
2016-09-02 16:15:11 -07:00
|
|
|
import com.facebook.react.module.annotations.ReactModule;
|
2017-08-02 19:59:54 -07:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.channels.FileChannel;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.annotation.Nullable;
|
2019-01-28 07:31:29 -08:00
|
|
|
import java.net.URLConnection;
|
2016-01-14 04:31:51 -08:00
|
|
|
|
|
|
|
// TODO #6015104: rename to something less iOSish
|
|
|
|
/**
|
2019-01-28 07:31:29 -08:00
|
|
|
* {@link NativeModule} that allows JS to interact with the photos and videos on the device (i.e.
|
2016-01-14 04:31:51 -08:00
|
|
|
* {@link MediaStore.Images}).
|
|
|
|
*/
|
2017-01-31 04:49:58 -08:00
|
|
|
@ReactModule(name = CameraRollManager.NAME)
|
2016-01-14 04:31:51 -08:00
|
|
|
public class CameraRollManager extends ReactContextBaseJavaModule {
|
|
|
|
|
2018-11-09 10:48:49 -08:00
|
|
|
public static final String NAME = "CameraRollManager";
|
2017-01-31 04:49:58 -08:00
|
|
|
|
2016-01-21 08:07:01 -08:00
|
|
|
private static final String ERROR_UNABLE_TO_LOAD = "E_UNABLE_TO_LOAD";
|
|
|
|
private static final String ERROR_UNABLE_TO_LOAD_PERMISSION = "E_UNABLE_TO_LOAD_PERMISSION";
|
|
|
|
private static final String ERROR_UNABLE_TO_SAVE = "E_UNABLE_TO_SAVE";
|
2019-01-28 07:31:29 -08:00
|
|
|
private static final String ERROR_UNABLE_TO_FILTER = "E_UNABLE_TO_FILTER";
|
|
|
|
|
|
|
|
private static final String ASSET_TYPE_PHOTOS = "Photos";
|
|
|
|
private static final String ASSET_TYPE_VIDEOS = "Videos";
|
|
|
|
private static final String ASSET_TYPE_ALL = "All";
|
|
|
|
|
2016-01-14 04:31:51 -08:00
|
|
|
|
|
|
|
public static final boolean IS_JELLY_BEAN_OR_LATER =
|
|
|
|
Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN;
|
|
|
|
|
|
|
|
private static final String[] PROJECTION;
|
|
|
|
static {
|
|
|
|
if (IS_JELLY_BEAN_OR_LATER) {
|
|
|
|
PROJECTION = new String[] {
|
|
|
|
Images.Media._ID,
|
|
|
|
Images.Media.MIME_TYPE,
|
|
|
|
Images.Media.BUCKET_DISPLAY_NAME,
|
|
|
|
Images.Media.DATE_TAKEN,
|
2019-01-28 07:31:29 -08:00
|
|
|
MediaStore.MediaColumns.WIDTH,
|
|
|
|
MediaStore.MediaColumns.HEIGHT,
|
2016-01-14 04:31:51 -08:00
|
|
|
Images.Media.LONGITUDE,
|
2019-01-28 07:31:29 -08:00
|
|
|
Images.Media.LATITUDE,
|
|
|
|
MediaStore.MediaColumns.DATA
|
2016-01-14 04:31:51 -08:00
|
|
|
};
|
|
|
|
} else {
|
|
|
|
PROJECTION = new String[] {
|
|
|
|
Images.Media._ID,
|
|
|
|
Images.Media.MIME_TYPE,
|
|
|
|
Images.Media.BUCKET_DISPLAY_NAME,
|
|
|
|
Images.Media.DATE_TAKEN,
|
|
|
|
Images.Media.LONGITUDE,
|
2019-01-28 07:31:29 -08:00
|
|
|
Images.Media.LATITUDE,
|
|
|
|
MediaStore.MediaColumns.DATA
|
2016-01-14 04:31:51 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final String SELECTION_BUCKET = Images.Media.BUCKET_DISPLAY_NAME + " = ?";
|
|
|
|
private static final String SELECTION_DATE_TAKEN = Images.Media.DATE_TAKEN + " < ?";
|
|
|
|
|
|
|
|
public CameraRollManager(ReactApplicationContext reactContext) {
|
|
|
|
super(reactContext);
|
|
|
|
}
|
|
|
|
|
2016-08-12 15:49:13 -07:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
2017-01-31 04:49:58 -08:00
|
|
|
return NAME;
|
2016-08-12 15:49:13 -07:00
|
|
|
}
|
|
|
|
|
2016-01-14 04:31:51 -08:00
|
|
|
/**
|
|
|
|
* Save an image to the gallery (i.e. {@link MediaStore.Images}). This copies the original file
|
|
|
|
* from wherever it may be to the external storage pictures directory, so that it can be scanned
|
|
|
|
* by the MediaScanner.
|
|
|
|
*
|
|
|
|
* @param uri the file:// URI of the image to save
|
2016-01-21 08:07:01 -08:00
|
|
|
* @param promise to be resolved or rejected
|
2016-01-14 04:31:51 -08:00
|
|
|
*/
|
|
|
|
@ReactMethod
|
Allow CameraRoll to export videos
Summary:
This PR adds the ability to export videos to the CameraRoll on both Android and iOS (previously only photos were possible, at least on iOS). The API has changed as follows:
```
// old
saveImageWithTag(tag: string): Promise<string>
// new
saveToCameraRoll(tag: string, type?: 'photo' | 'video'): Promise<string>
```
if no `type` parameter is passed, `video` is inferred if the tag ends with ".mov" or ".mp4", otherwise `photo` is assumed.
I've left in the `saveImageWithTag` method for now with a deprecation warning.
**Test plan (required)**
I created the following very simple app to test exporting photos and videos to the CameraRoll, and ran it on both iOS and Android. The functionality works as intended on both platforms.
```js
// index.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
CameraRoll,
} from 'react-native';
import FS fro
Closes https://github.com/facebook/react-native/pull/7988
Differential Revision: D3401251
Pulled By: nicklockwood
fbshipit-source-id: af3fc24e6fa5b84ac377e9173f3709c6f9795f20
2016-06-07 16:37:48 -07:00
|
|
|
public void saveToCameraRoll(String uri, String type, Promise promise) {
|
2017-02-02 03:59:50 -08:00
|
|
|
new SaveToCameraRoll(getReactApplicationContext(), Uri.parse(uri), promise)
|
2016-01-14 04:31:51 -08:00
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
|
Allow CameraRoll to export videos
Summary:
This PR adds the ability to export videos to the CameraRoll on both Android and iOS (previously only photos were possible, at least on iOS). The API has changed as follows:
```
// old
saveImageWithTag(tag: string): Promise<string>
// new
saveToCameraRoll(tag: string, type?: 'photo' | 'video'): Promise<string>
```
if no `type` parameter is passed, `video` is inferred if the tag ends with ".mov" or ".mp4", otherwise `photo` is assumed.
I've left in the `saveImageWithTag` method for now with a deprecation warning.
**Test plan (required)**
I created the following very simple app to test exporting photos and videos to the CameraRoll, and ran it on both iOS and Android. The functionality works as intended on both platforms.
```js
// index.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
CameraRoll,
} from 'react-native';
import FS fro
Closes https://github.com/facebook/react-native/pull/7988
Differential Revision: D3401251
Pulled By: nicklockwood
fbshipit-source-id: af3fc24e6fa5b84ac377e9173f3709c6f9795f20
2016-06-07 16:37:48 -07:00
|
|
|
private static class SaveToCameraRoll extends GuardedAsyncTask<Void, Void> {
|
2016-01-14 04:31:51 -08:00
|
|
|
|
|
|
|
private final Context mContext;
|
|
|
|
private final Uri mUri;
|
2016-01-21 08:07:01 -08:00
|
|
|
private final Promise mPromise;
|
2016-01-14 04:31:51 -08:00
|
|
|
|
2017-02-02 03:59:50 -08:00
|
|
|
public SaveToCameraRoll(ReactContext context, Uri uri, Promise promise) {
|
2016-01-14 04:31:51 -08:00
|
|
|
super(context);
|
|
|
|
mContext = context;
|
|
|
|
mUri = uri;
|
2016-01-21 08:07:01 -08:00
|
|
|
mPromise = promise;
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doInBackgroundGuarded(Void... params) {
|
|
|
|
File source = new File(mUri.getPath());
|
|
|
|
FileChannel input = null, output = null;
|
|
|
|
try {
|
2017-02-02 03:59:50 -08:00
|
|
|
File exportDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
|
Allow CameraRoll to export videos
Summary:
This PR adds the ability to export videos to the CameraRoll on both Android and iOS (previously only photos were possible, at least on iOS). The API has changed as follows:
```
// old
saveImageWithTag(tag: string): Promise<string>
// new
saveToCameraRoll(tag: string, type?: 'photo' | 'video'): Promise<string>
```
if no `type` parameter is passed, `video` is inferred if the tag ends with ".mov" or ".mp4", otherwise `photo` is assumed.
I've left in the `saveImageWithTag` method for now with a deprecation warning.
**Test plan (required)**
I created the following very simple app to test exporting photos and videos to the CameraRoll, and ran it on both iOS and Android. The functionality works as intended on both platforms.
```js
// index.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
CameraRoll,
} from 'react-native';
import FS fro
Closes https://github.com/facebook/react-native/pull/7988
Differential Revision: D3401251
Pulled By: nicklockwood
fbshipit-source-id: af3fc24e6fa5b84ac377e9173f3709c6f9795f20
2016-06-07 16:37:48 -07:00
|
|
|
exportDir.mkdirs();
|
|
|
|
if (!exportDir.isDirectory()) {
|
|
|
|
mPromise.reject(ERROR_UNABLE_TO_LOAD, "External media storage directory not available");
|
2016-01-14 04:31:51 -08:00
|
|
|
return;
|
|
|
|
}
|
Allow CameraRoll to export videos
Summary:
This PR adds the ability to export videos to the CameraRoll on both Android and iOS (previously only photos were possible, at least on iOS). The API has changed as follows:
```
// old
saveImageWithTag(tag: string): Promise<string>
// new
saveToCameraRoll(tag: string, type?: 'photo' | 'video'): Promise<string>
```
if no `type` parameter is passed, `video` is inferred if the tag ends with ".mov" or ".mp4", otherwise `photo` is assumed.
I've left in the `saveImageWithTag` method for now with a deprecation warning.
**Test plan (required)**
I created the following very simple app to test exporting photos and videos to the CameraRoll, and ran it on both iOS and Android. The functionality works as intended on both platforms.
```js
// index.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
CameraRoll,
} from 'react-native';
import FS fro
Closes https://github.com/facebook/react-native/pull/7988
Differential Revision: D3401251
Pulled By: nicklockwood
fbshipit-source-id: af3fc24e6fa5b84ac377e9173f3709c6f9795f20
2016-06-07 16:37:48 -07:00
|
|
|
File dest = new File(exportDir, source.getName());
|
2016-01-14 04:31:51 -08:00
|
|
|
int n = 0;
|
|
|
|
String fullSourceName = source.getName();
|
|
|
|
String sourceName, sourceExt;
|
|
|
|
if (fullSourceName.indexOf('.') >= 0) {
|
|
|
|
sourceName = fullSourceName.substring(0, fullSourceName.lastIndexOf('.'));
|
|
|
|
sourceExt = fullSourceName.substring(fullSourceName.lastIndexOf('.'));
|
|
|
|
} else {
|
|
|
|
sourceName = fullSourceName;
|
|
|
|
sourceExt = "";
|
|
|
|
}
|
|
|
|
while (!dest.createNewFile()) {
|
Allow CameraRoll to export videos
Summary:
This PR adds the ability to export videos to the CameraRoll on both Android and iOS (previously only photos were possible, at least on iOS). The API has changed as follows:
```
// old
saveImageWithTag(tag: string): Promise<string>
// new
saveToCameraRoll(tag: string, type?: 'photo' | 'video'): Promise<string>
```
if no `type` parameter is passed, `video` is inferred if the tag ends with ".mov" or ".mp4", otherwise `photo` is assumed.
I've left in the `saveImageWithTag` method for now with a deprecation warning.
**Test plan (required)**
I created the following very simple app to test exporting photos and videos to the CameraRoll, and ran it on both iOS and Android. The functionality works as intended on both platforms.
```js
// index.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
CameraRoll,
} from 'react-native';
import FS fro
Closes https://github.com/facebook/react-native/pull/7988
Differential Revision: D3401251
Pulled By: nicklockwood
fbshipit-source-id: af3fc24e6fa5b84ac377e9173f3709c6f9795f20
2016-06-07 16:37:48 -07:00
|
|
|
dest = new File(exportDir, sourceName + "_" + (n++) + sourceExt);
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
input = new FileInputStream(source).getChannel();
|
|
|
|
output = new FileOutputStream(dest).getChannel();
|
|
|
|
output.transferFrom(input, 0, input.size());
|
|
|
|
input.close();
|
|
|
|
output.close();
|
|
|
|
|
|
|
|
MediaScannerConnection.scanFile(
|
|
|
|
mContext,
|
|
|
|
new String[]{dest.getAbsolutePath()},
|
|
|
|
null,
|
|
|
|
new MediaScannerConnection.OnScanCompletedListener() {
|
|
|
|
@Override
|
|
|
|
public void onScanCompleted(String path, Uri uri) {
|
|
|
|
if (uri != null) {
|
2016-01-21 08:07:01 -08:00
|
|
|
mPromise.resolve(uri.toString());
|
2016-01-14 04:31:51 -08:00
|
|
|
} else {
|
2016-01-26 10:29:47 -08:00
|
|
|
mPromise.reject(ERROR_UNABLE_TO_SAVE, "Could not add image to gallery");
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} catch (IOException e) {
|
2016-01-21 08:07:01 -08:00
|
|
|
mPromise.reject(e);
|
2016-01-14 04:31:51 -08:00
|
|
|
} finally {
|
|
|
|
if (input != null && input.isOpen()) {
|
|
|
|
try {
|
|
|
|
input.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
FLog.e(ReactConstants.TAG, "Could not close input channel", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (output != null && output.isOpen()) {
|
|
|
|
try {
|
|
|
|
output.close();
|
|
|
|
} catch (IOException e) {
|
|
|
|
FLog.e(ReactConstants.TAG, "Could not close output channel", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get photos from {@link MediaStore.Images}, most recent first.
|
|
|
|
*
|
|
|
|
* @param params a map containing the following keys:
|
|
|
|
* <ul>
|
|
|
|
* <li>first (mandatory): a number representing the number of photos to fetch</li>
|
|
|
|
* <li>
|
|
|
|
* after (optional): a cursor that matches page_info[end_cursor] returned by a
|
|
|
|
* previous call to {@link #getPhotos}
|
|
|
|
* </li>
|
|
|
|
* <li>groupName (optional): an album name</li>
|
|
|
|
* <li>
|
|
|
|
* mimeType (optional): restrict returned images to a specific mimetype (e.g.
|
|
|
|
* image/jpeg)
|
|
|
|
* </li>
|
2017-05-10 03:17:11 -07:00
|
|
|
* <li>
|
|
|
|
* assetType (optional): chooses between either photos or videos from the camera roll.
|
|
|
|
* Valid values are "Photos" or "Videos". Defaults to photos.
|
|
|
|
* </li>
|
2016-01-14 04:31:51 -08:00
|
|
|
* </ul>
|
2016-01-21 08:07:01 -08:00
|
|
|
* @param promise the Promise to be resolved when the photos are loaded; for a format of the
|
2016-01-14 04:31:51 -08:00
|
|
|
* parameters passed to this callback, see {@code getPhotosReturnChecker} in CameraRoll.js
|
|
|
|
*/
|
|
|
|
@ReactMethod
|
2016-01-21 08:07:01 -08:00
|
|
|
public void getPhotos(final ReadableMap params, final Promise promise) {
|
2016-01-14 04:31:51 -08:00
|
|
|
int first = params.getInt("first");
|
|
|
|
String after = params.hasKey("after") ? params.getString("after") : null;
|
|
|
|
String groupName = params.hasKey("groupName") ? params.getString("groupName") : null;
|
2019-01-28 07:31:29 -08:00
|
|
|
String assetType = params.hasKey("assetType") ? params.getString("assetType") : ASSET_TYPE_PHOTOS;
|
2016-01-14 04:31:51 -08:00
|
|
|
ReadableArray mimeTypes = params.hasKey("mimeTypes")
|
|
|
|
? params.getArray("mimeTypes")
|
|
|
|
: null;
|
|
|
|
if (params.hasKey("groupTypes")) {
|
|
|
|
throw new JSApplicationIllegalArgumentException("groupTypes is not supported on Android");
|
|
|
|
}
|
|
|
|
|
2019-01-28 07:31:29 -08:00
|
|
|
new GetMediaTask(
|
2016-01-14 04:31:51 -08:00
|
|
|
getReactApplicationContext(),
|
|
|
|
first,
|
|
|
|
after,
|
|
|
|
groupName,
|
|
|
|
mimeTypes,
|
2017-05-10 03:17:11 -07:00
|
|
|
assetType,
|
2016-01-21 08:07:01 -08:00
|
|
|
promise)
|
2016-01-14 04:31:51 -08:00
|
|
|
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
|
|
|
|
}
|
|
|
|
|
2019-01-28 07:31:29 -08:00
|
|
|
private static class GetMediaTask extends GuardedAsyncTask<Void, Void> {
|
2016-01-14 04:31:51 -08:00
|
|
|
private final Context mContext;
|
|
|
|
private final int mFirst;
|
|
|
|
private final @Nullable String mAfter;
|
|
|
|
private final @Nullable String mGroupName;
|
|
|
|
private final @Nullable ReadableArray mMimeTypes;
|
2016-01-21 08:07:01 -08:00
|
|
|
private final Promise mPromise;
|
2019-01-28 07:31:29 -08:00
|
|
|
private final String mAssetType;
|
2016-01-14 04:31:51 -08:00
|
|
|
|
2019-01-28 07:31:29 -08:00
|
|
|
private GetMediaTask(
|
2016-01-14 04:31:51 -08:00
|
|
|
ReactContext context,
|
|
|
|
int first,
|
|
|
|
@Nullable String after,
|
|
|
|
@Nullable String groupName,
|
|
|
|
@Nullable ReadableArray mimeTypes,
|
2019-01-28 07:31:29 -08:00
|
|
|
String assetType,
|
2016-01-21 08:07:01 -08:00
|
|
|
Promise promise) {
|
2016-01-14 04:31:51 -08:00
|
|
|
super(context);
|
|
|
|
mContext = context;
|
|
|
|
mFirst = first;
|
|
|
|
mAfter = after;
|
|
|
|
mGroupName = groupName;
|
|
|
|
mMimeTypes = mimeTypes;
|
2016-01-21 08:07:01 -08:00
|
|
|
mPromise = promise;
|
2017-05-10 03:17:11 -07:00
|
|
|
mAssetType = assetType;
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void doInBackgroundGuarded(Void... params) {
|
|
|
|
StringBuilder selection = new StringBuilder("1");
|
|
|
|
List<String> selectionArgs = new ArrayList<>();
|
|
|
|
if (!TextUtils.isEmpty(mAfter)) {
|
|
|
|
selection.append(" AND " + SELECTION_DATE_TAKEN);
|
|
|
|
selectionArgs.add(mAfter);
|
|
|
|
}
|
|
|
|
if (!TextUtils.isEmpty(mGroupName)) {
|
|
|
|
selection.append(" AND " + SELECTION_BUCKET);
|
|
|
|
selectionArgs.add(mGroupName);
|
|
|
|
}
|
2019-01-28 07:31:29 -08:00
|
|
|
|
|
|
|
if (mAssetType.equals(ASSET_TYPE_PHOTOS)) {
|
|
|
|
selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = "
|
|
|
|
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE);
|
|
|
|
} else if (mAssetType.equals(ASSET_TYPE_VIDEOS)) {
|
|
|
|
selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " = "
|
|
|
|
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO);
|
|
|
|
} else if (mAssetType.equals(ASSET_TYPE_ALL)) {
|
|
|
|
selection.append(" AND " + MediaStore.Files.FileColumns.MEDIA_TYPE + " IN ("
|
|
|
|
+ MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO + ","
|
|
|
|
+ MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE + ")");
|
|
|
|
} else {
|
|
|
|
mPromise.reject(
|
|
|
|
ERROR_UNABLE_TO_FILTER,
|
|
|
|
"Invalid filter option: '" + mAssetType + "'. Expected one of '"
|
|
|
|
+ ASSET_TYPE_PHOTOS + "', '" + ASSET_TYPE_VIDEOS + "' or '" + ASSET_TYPE_ALL + "'."
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-14 04:31:51 -08:00
|
|
|
if (mMimeTypes != null && mMimeTypes.size() > 0) {
|
|
|
|
selection.append(" AND " + Images.Media.MIME_TYPE + " IN (");
|
|
|
|
for (int i = 0; i < mMimeTypes.size(); i++) {
|
|
|
|
selection.append("?,");
|
|
|
|
selectionArgs.add(mMimeTypes.getString(i));
|
|
|
|
}
|
|
|
|
selection.replace(selection.length() - 1, selection.length(), ")");
|
|
|
|
}
|
|
|
|
WritableMap response = new WritableNativeMap();
|
|
|
|
ContentResolver resolver = mContext.getContentResolver();
|
|
|
|
// using LIMIT in the sortOrder is not explicitly supported by the SDK (which does not support
|
|
|
|
// setting a limit at all), but it works because this specific ContentProvider is backed by
|
|
|
|
// an SQLite DB and forwards parameters to it without doing any parsing / validation.
|
|
|
|
try {
|
2019-01-28 07:31:29 -08:00
|
|
|
Cursor media = resolver.query(
|
|
|
|
MediaStore.Files.getContentUri("external"),
|
2016-01-14 04:31:51 -08:00
|
|
|
PROJECTION,
|
|
|
|
selection.toString(),
|
|
|
|
selectionArgs.toArray(new String[selectionArgs.size()]),
|
|
|
|
Images.Media.DATE_TAKEN + " DESC, " + Images.Media.DATE_MODIFIED + " DESC LIMIT " +
|
|
|
|
(mFirst + 1)); // set LIMIT to first + 1 so that we know how to populate page_info
|
2019-01-28 07:31:29 -08:00
|
|
|
if (media == null) {
|
|
|
|
mPromise.reject(ERROR_UNABLE_TO_LOAD, "Could not get media");
|
2016-01-14 04:31:51 -08:00
|
|
|
} else {
|
|
|
|
try {
|
2019-01-28 07:31:29 -08:00
|
|
|
putEdges(resolver, media, response, mFirst);
|
|
|
|
putPageInfo(media, response, mFirst);
|
2016-01-14 04:31:51 -08:00
|
|
|
} finally {
|
2019-01-28 07:31:29 -08:00
|
|
|
media.close();
|
2016-01-21 08:07:01 -08:00
|
|
|
mPromise.resolve(response);
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (SecurityException e) {
|
2016-01-21 08:07:01 -08:00
|
|
|
mPromise.reject(
|
|
|
|
ERROR_UNABLE_TO_LOAD_PERMISSION,
|
2019-01-28 07:31:29 -08:00
|
|
|
"Could not get media: need READ_EXTERNAL_STORAGE permission",
|
2016-01-21 08:07:01 -08:00
|
|
|
e);
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-28 07:31:29 -08:00
|
|
|
private static void putPageInfo(Cursor media, WritableMap response, int limit) {
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableMap pageInfo = new WritableNativeMap();
|
2019-01-28 07:31:29 -08:00
|
|
|
pageInfo.putBoolean("has_next_page", limit < media.getCount());
|
|
|
|
if (limit < media.getCount()) {
|
|
|
|
media.moveToPosition(limit - 1);
|
2016-01-14 04:31:51 -08:00
|
|
|
pageInfo.putString(
|
|
|
|
"end_cursor",
|
2019-01-28 07:31:29 -08:00
|
|
|
media.getString(media.getColumnIndex(Images.Media.DATE_TAKEN)));
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
response.putMap("page_info", pageInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void putEdges(
|
|
|
|
ContentResolver resolver,
|
2019-01-28 07:31:29 -08:00
|
|
|
Cursor media,
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableMap response,
|
2019-01-28 07:31:29 -08:00
|
|
|
int limit) {
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableArray edges = new WritableNativeArray();
|
2019-01-28 07:31:29 -08:00
|
|
|
media.moveToFirst();
|
|
|
|
int idIndex = media.getColumnIndex(Images.Media._ID);
|
|
|
|
int mimeTypeIndex = media.getColumnIndex(Images.Media.MIME_TYPE);
|
|
|
|
int groupNameIndex = media.getColumnIndex(Images.Media.BUCKET_DISPLAY_NAME);
|
|
|
|
int dateTakenIndex = media.getColumnIndex(Images.Media.DATE_TAKEN);
|
|
|
|
int widthIndex = IS_JELLY_BEAN_OR_LATER ? media.getColumnIndex(MediaStore.MediaColumns.WIDTH) : -1;
|
|
|
|
int heightIndex = IS_JELLY_BEAN_OR_LATER ? media.getColumnIndex(MediaStore.MediaColumns.HEIGHT) : -1;
|
|
|
|
int longitudeIndex = media.getColumnIndex(Images.Media.LONGITUDE);
|
|
|
|
int latitudeIndex = media.getColumnIndex(Images.Media.LATITUDE);
|
|
|
|
int dataIndex = media.getColumnIndex(MediaStore.MediaColumns.DATA);
|
|
|
|
|
|
|
|
for (int i = 0; i < limit && !media.isAfterLast(); i++) {
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableMap edge = new WritableNativeMap();
|
|
|
|
WritableMap node = new WritableNativeMap();
|
|
|
|
boolean imageInfoSuccess =
|
2019-01-28 07:31:29 -08:00
|
|
|
putImageInfo(resolver, media, node, idIndex, widthIndex, heightIndex, dataIndex);
|
2016-01-14 04:31:51 -08:00
|
|
|
if (imageInfoSuccess) {
|
2019-01-28 07:31:29 -08:00
|
|
|
putBasicNodeInfo(media, node, mimeTypeIndex, groupNameIndex, dateTakenIndex);
|
|
|
|
putLocationInfo(media, node, longitudeIndex, latitudeIndex);
|
2016-01-14 04:31:51 -08:00
|
|
|
|
|
|
|
edge.putMap("node", node);
|
|
|
|
edges.pushMap(edge);
|
|
|
|
} else {
|
|
|
|
// we skipped an image because we couldn't get its details (e.g. width/height), so we
|
|
|
|
// decrement i in order to correctly reach the limit, if the cursor has enough rows
|
|
|
|
i--;
|
|
|
|
}
|
2019-01-28 07:31:29 -08:00
|
|
|
media.moveToNext();
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
response.putArray("edges", edges);
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void putBasicNodeInfo(
|
2019-01-28 07:31:29 -08:00
|
|
|
Cursor media,
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableMap node,
|
|
|
|
int mimeTypeIndex,
|
|
|
|
int groupNameIndex,
|
|
|
|
int dateTakenIndex) {
|
2019-01-28 07:31:29 -08:00
|
|
|
node.putString("type", media.getString(mimeTypeIndex));
|
|
|
|
node.putString("group_name", media.getString(groupNameIndex));
|
|
|
|
node.putDouble("timestamp", media.getLong(dateTakenIndex) / 1000d);
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean putImageInfo(
|
|
|
|
ContentResolver resolver,
|
2019-01-28 07:31:29 -08:00
|
|
|
Cursor media,
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableMap node,
|
|
|
|
int idIndex,
|
|
|
|
int widthIndex,
|
2017-08-02 19:59:54 -07:00
|
|
|
int heightIndex,
|
2019-01-28 07:31:29 -08:00
|
|
|
int dataIndex) {
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableMap image = new WritableNativeMap();
|
2019-01-28 07:31:29 -08:00
|
|
|
Uri photoUri = Uri.parse("file://" + media.getString(dataIndex));
|
2016-01-14 04:31:51 -08:00
|
|
|
image.putString("uri", photoUri.toString());
|
|
|
|
float width = -1;
|
|
|
|
float height = -1;
|
|
|
|
if (IS_JELLY_BEAN_OR_LATER) {
|
2019-01-28 07:31:29 -08:00
|
|
|
width = media.getInt(widthIndex);
|
|
|
|
height = media.getInt(heightIndex);
|
2016-01-14 04:31:51 -08:00
|
|
|
}
|
2017-08-15 11:36:44 -07:00
|
|
|
|
2019-01-28 07:31:29 -08:00
|
|
|
String mimeType = URLConnection.guessContentTypeFromName(photoUri.toString());
|
|
|
|
|
|
|
|
if (mimeType != null
|
|
|
|
&& mimeType.startsWith("video")
|
2017-08-15 11:36:44 -07:00
|
|
|
&& android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.GINGERBREAD_MR1) {
|
2016-01-14 04:31:51 -08:00
|
|
|
try {
|
|
|
|
AssetFileDescriptor photoDescriptor = resolver.openAssetFileDescriptor(photoUri, "r");
|
2017-08-15 11:36:44 -07:00
|
|
|
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
|
|
|
|
retriever.setDataSource(photoDescriptor.getFileDescriptor());
|
|
|
|
|
2017-10-09 22:34:40 -07:00
|
|
|
try {
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
width =
|
|
|
|
Integer.parseInt(
|
|
|
|
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
|
|
|
|
height =
|
|
|
|
Integer.parseInt(
|
|
|
|
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
|
|
|
|
}
|
|
|
|
int timeInMillisec =
|
2017-08-15 11:36:42 -07:00
|
|
|
Integer.parseInt(
|
2017-10-09 22:34:40 -07:00
|
|
|
retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
|
|
|
|
int playableDuration = timeInMillisec / 1000;
|
|
|
|
image.putInt("playableDuration", playableDuration);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
FLog.e(
|
|
|
|
ReactConstants.TAG,
|
|
|
|
"Number format exception occurred while trying to fetch video metadata for "
|
|
|
|
+ photoUri.toString(),
|
|
|
|
e);
|
|
|
|
return false;
|
|
|
|
} finally {
|
|
|
|
retriever.release();
|
|
|
|
photoDescriptor.close();
|
2017-08-15 11:36:42 -07:00
|
|
|
}
|
2018-10-01 14:24:59 -07:00
|
|
|
} catch (Exception e) {
|
2017-08-15 11:36:44 -07:00
|
|
|
FLog.e(ReactConstants.TAG, "Could not get video metadata for " + photoUri.toString(), e);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (width <= 0 || height <= 0) {
|
|
|
|
try {
|
|
|
|
AssetFileDescriptor photoDescriptor = resolver.openAssetFileDescriptor(photoUri, "r");
|
|
|
|
BitmapFactory.Options options = new BitmapFactory.Options();
|
|
|
|
// Set inJustDecodeBounds to true so we don't actually load the Bitmap, but only get its
|
|
|
|
// dimensions instead.
|
|
|
|
options.inJustDecodeBounds = true;
|
|
|
|
BitmapFactory.decodeFileDescriptor(photoDescriptor.getFileDescriptor(), null, options);
|
|
|
|
width = options.outWidth;
|
|
|
|
height = options.outHeight;
|
2016-01-14 04:31:51 -08:00
|
|
|
photoDescriptor.close();
|
|
|
|
} catch (IOException e) {
|
2016-01-26 10:29:47 -08:00
|
|
|
FLog.e(ReactConstants.TAG, "Could not get width/height for " + photoUri.toString(), e);
|
2016-01-14 04:31:51 -08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
image.putDouble("width", width);
|
|
|
|
image.putDouble("height", height);
|
|
|
|
node.putMap("image", image);
|
2019-01-28 07:31:29 -08:00
|
|
|
|
2016-01-14 04:31:51 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void putLocationInfo(
|
2019-01-28 07:31:29 -08:00
|
|
|
Cursor media,
|
2016-01-14 04:31:51 -08:00
|
|
|
WritableMap node,
|
|
|
|
int longitudeIndex,
|
|
|
|
int latitudeIndex) {
|
2019-01-28 07:31:29 -08:00
|
|
|
double longitude = media.getDouble(longitudeIndex);
|
|
|
|
double latitude = media.getDouble(latitudeIndex);
|
2016-01-14 04:31:51 -08:00
|
|
|
if (longitude > 0 || latitude > 0) {
|
|
|
|
WritableMap location = new WritableNativeMap();
|
|
|
|
location.putDouble("longitude", longitude);
|
|
|
|
location.putDouble("latitude", latitude);
|
|
|
|
node.putMap("location", location);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|