Android infra, getAlbumsWithThumbnails working

This commit is contained in:
Yedidya Kennard 2016-06-29 18:31:04 +03:00
parent e539c0281c
commit 22e2e5e6ec
6 changed files with 217 additions and 0 deletions

23
android/build.gradle Normal file
View File

@ -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.+'
}

View File

@ -0,0 +1,3 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wix.RNCameraKit">
</manifest>

View File

@ -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<String, Album> 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<Album> 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) {
}
}

View File

@ -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<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
List<NativeModule> modules = new ArrayList<>();
modules.add(new NativeGalleryManager(reactContext));
return modules;
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
}

12
index.android.js Normal file
View File

@ -0,0 +1,12 @@
import CameraKitGallery from './src/CameraKitGallery';
//import CameraKitCamera from './src/CameraKitCamera';
//import CameraKitGalleryView from './src/CameraKitGalleryView';
export {
CameraKitGallery,
//CameraKitCamera,
//CameraKitGalleryView
};

View File

@ -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
}