diff --git a/android/build.gradle b/android/build.gradle new file mode 100644 index 0000000..a77e79f --- /dev/null +++ b/android/build.gradle @@ -0,0 +1,23 @@ +apply plugin: 'com.android.library' + +android { + compileSdkVersion 23 + buildToolsVersion "23.0.3" + + defaultConfig { + minSdkVersion 16 + targetSdkVersion 22 + versionCode 1 + versionName "1.0" + ndk { + abiFilters "armeabi-v7a", "x86" + } + } + lintOptions { + warning 'InvalidPackage' + } +} + +dependencies { + compile 'com.facebook.react:react-native:0.20.+' +} \ No newline at end of file diff --git a/android/src/main/AndroidManifest.xml b/android/src/main/AndroidManifest.xml new file mode 100644 index 0000000..8d85824 --- /dev/null +++ b/android/src/main/AndroidManifest.xml @@ -0,0 +1,3 @@ + + \ No newline at end of file diff --git a/android/src/main/java/com/wix/RNCameraKit/NativeGalleryManager.java b/android/src/main/java/com/wix/RNCameraKit/NativeGalleryManager.java new file mode 100644 index 0000000..d062f13 --- /dev/null +++ b/android/src/main/java/com/wix/RNCameraKit/NativeGalleryManager.java @@ -0,0 +1,129 @@ +package com.wix.RNCameraKit; + +import android.database.Cursor; +import android.graphics.Bitmap; +import android.net.Uri; +import android.provider.MediaStore; +import android.util.Base64; + +import com.facebook.react.bridge.Arguments; +import com.facebook.react.bridge.Promise; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.bridge.ReactContextBaseJavaModule; +import com.facebook.react.bridge.ReactMethod; +import com.facebook.react.bridge.WritableArray; +import com.facebook.react.bridge.WritableMap; + +import java.io.ByteArrayOutputStream; +import java.util.Collection; +import java.util.HashMap; + +/** + * Created by yedidyak on 29/06/2016. + */ +public class NativeGalleryManager extends ReactContextBaseJavaModule { + + private class Album { + String name; + String imageData = null; + int count = 1; + + public Album(String name) { + this.name = name; + } + } + + private class AlbumList { + HashMap albums = new HashMap<>(); + + public void addAlbum(String name) { + if (!albums.containsKey(name)) { + albums.put(name, new Album(name)); + } + else { + albums.get(name).count++; + } + } + + public void setThumbnail(String name, Bitmap thumbnail) { + + ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); + thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream); + byte[] byteArray = byteArrayOutputStream .toByteArray(); + String encoded = Base64.encodeToString(byteArray, Base64.DEFAULT); + + albums.get(name).imageData = encoded; + } + + public boolean hasThumbnail(String name) { + return albums.get(name).imageData != null; + } + + public Collection getAlbums() { + return albums.values(); + } + } + + public NativeGalleryManager(ReactApplicationContext reactContext) { + super(reactContext); + } + + @Override + public String getName() { + return "NativeGalleryManager"; + } + + @ReactMethod + public void getAlbumsWithThumbnails(Promise promise) { + + String[] projection = new String[]{ + MediaStore.Images.Media._ID, + MediaStore.Images.Media.BUCKET_DISPLAY_NAME, + MediaStore.Images.Media._ID + }; + + Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; + Cursor imagesCursor = getCurrentActivity().getContentResolver().query(images, projection, null, null, null); + + AlbumList albums = new AlbumList(); + + if (imagesCursor.moveToFirst()) { + int bucketColumn = imagesCursor.getColumnIndex(MediaStore.Images.Media.BUCKET_DISPLAY_NAME); + int thumbIdColumn = imagesCursor.getColumnIndex(MediaStore.Images.Media._ID); + do { + String name = imagesCursor.getString(bucketColumn); + albums.addAlbum(name); + + if(!albums.hasThumbnail(name)) { + int thumbId = imagesCursor.getInt(thumbIdColumn); + Bitmap thumb = MediaStore.Images.Thumbnails.getThumbnail( + getCurrentActivity().getContentResolver(), + thumbId, + MediaStore.Images.Thumbnails.MINI_KIND, + null); + albums.setThumbnail(name, thumb); + } + } while (imagesCursor.moveToNext()); + } + + WritableArray arr = Arguments.createArray(); + + for (Album album : albums.getAlbums()) { + WritableMap map = Arguments.createMap(); + map.putInt("imagesCount", album.count); + map.putString("albumName", album.name); + map.putString("image", album.imageData); + arr.pushMap(map); + } + + WritableMap ret = Arguments.createMap(); + ret.putArray("albums", arr); + + promise.resolve(ret); + } + + @ReactMethod + public void getPhotosForAlbum(String albumName, int numberOfPhotos, Promise promise) { + + } +} diff --git a/android/src/main/java/com/wix/RNCameraKit/RNCameraKitPackage.java b/android/src/main/java/com/wix/RNCameraKit/RNCameraKitPackage.java new file mode 100644 index 0000000..1786d2e --- /dev/null +++ b/android/src/main/java/com/wix/RNCameraKit/RNCameraKitPackage.java @@ -0,0 +1,32 @@ +package com.wix.RNCameraKit; + +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.bridge.ReactApplicationContext; +import com.facebook.react.uimanager.ViewManager; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; + +public class RNCameraKitPackage implements ReactPackage { + + @Override + public List createNativeModules(ReactApplicationContext reactContext) { + List modules = new ArrayList<>(); + modules.add(new NativeGalleryManager(reactContext)); + return modules; + } + + @Override + public List> createJSModules() { + return Collections.emptyList(); + } + + @Override + public List createViewManagers(ReactApplicationContext reactContext) { + return Collections.emptyList(); + } + +} \ No newline at end of file diff --git a/index.android.js b/index.android.js new file mode 100644 index 0000000..4d2fdd0 --- /dev/null +++ b/index.android.js @@ -0,0 +1,12 @@ +import CameraKitGallery from './src/CameraKitGallery'; +//import CameraKitCamera from './src/CameraKitCamera'; +//import CameraKitGalleryView from './src/CameraKitGalleryView'; + +export { + CameraKitGallery, + //CameraKitCamera, + //CameraKitGalleryView +}; + + + diff --git a/src/CameraKitGallery.android.js b/src/CameraKitGallery.android.js new file mode 100644 index 0000000..d891143 --- /dev/null +++ b/src/CameraKitGallery.android.js @@ -0,0 +1,18 @@ +import {NativeModules} from 'react-native'; +const NativeGalleryManager = NativeModules.NativeGalleryManager; +import _ from 'lodash'; + +async function getAlbumsWithThumbnails() { + const albums = await NativeGalleryManager.getAlbumsWithThumbnails(); + return albums; +} + + +async function getPhotosForAlbum(albumName, numberOfPhotos) { + +} + +export default { + getAlbumsWithThumbnails, + getPhotosForAlbum +}