diff --git a/README.md b/README.md index ae8c5d92..a9705567 100644 --- a/README.md +++ b/README.md @@ -288,6 +288,7 @@ public class MainApplication extends Application implements ReactApplication { android:theme="@style/Theme.Share.Transparent" > + // for sharing links include diff --git a/android/src/main/java/com/alinz/parkerdan/shareextension/RealPathUtil.java b/android/src/main/java/com/alinz/parkerdan/shareextension/RealPathUtil.java index c712e18a..8bf985e2 100644 --- a/android/src/main/java/com/alinz/parkerdan/shareextension/RealPathUtil.java +++ b/android/src/main/java/com/alinz/parkerdan/shareextension/RealPathUtil.java @@ -9,6 +9,10 @@ import android.content.ContentUris; import android.os.Environment; +import android.os.ParcelFileDescriptor; +import java.io.*; +import java.nio.channels.FileChannel; + public class RealPathUtil { public static String getRealPathFromURI(final Context context, final Uri uri) { @@ -67,7 +71,11 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) { if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); - return getDataColumn(context, uri, null, null); + String path = getDataColumn(context, uri, null, null); + + if (path != null) return path; + // Try save to tmp file, and return tmp file path + return getPathFromSavingTempFile(context, uri); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { @@ -77,6 +85,25 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) { return null; } + public static String getPathFromSavingTempFile(Context context, final Uri uri) { + File tmpFile; + try { + String fileName = uri.getLastPathSegment(); + tmpFile = File.createTempFile("tmp", fileName, context.getCacheDir()); + + ParcelFileDescriptor pfd = context.getContentResolver().openFileDescriptor(uri, "r"); + + FileChannel src = new FileInputStream(pfd.getFileDescriptor()).getChannel(); + FileChannel dst = new FileOutputStream(tmpFile).getChannel(); + dst.transferFrom(src, 0, src.size()); + src.close(); + dst.close(); + } catch (IOException ex) { + return null; + } + return tmpFile.getAbsolutePath(); + } + /** * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. diff --git a/android/src/main/java/com/alinz/parkerdan/shareextension/ShareModule.java b/android/src/main/java/com/alinz/parkerdan/shareextension/ShareModule.java index e1aea2ce..f09c2299 100644 --- a/android/src/main/java/com/alinz/parkerdan/shareextension/ShareModule.java +++ b/android/src/main/java/com/alinz/parkerdan/shareextension/ShareModule.java @@ -5,6 +5,7 @@ import com.facebook.react.bridge.Promise; import com.facebook.react.bridge.ReactMethod; import com.facebook.react.bridge.WritableMap; +import com.facebook.react.bridge.WritableArray; import com.facebook.react.bridge.Arguments; import android.app.Activity; @@ -13,6 +14,7 @@ import android.graphics.Bitmap; import java.io.InputStream; +import java.util.ArrayList; public class ShareModule extends ReactContextBaseJavaModule { @@ -27,6 +29,18 @@ public String getName() { return "ReactNativeShareExtension"; } + @ReactMethod + public void clear() { + Activity currentActivity = getCurrentActivity(); + + if (currentActivity != null) { + Intent intent = currentActivity.getIntent(); + intent.setAction(""); + intent.removeExtra(Intent.EXTRA_TEXT); + intent.removeExtra(Intent.EXTRA_STREAM); + } + } + @ReactMethod public void close() { getCurrentActivity().finish(); @@ -39,8 +53,9 @@ public void data(Promise promise) { public WritableMap processIntent() { WritableMap map = Arguments.createMap(); + WritableArray images = Arguments.createArray(); - String value = ""; + String text = ""; String type = ""; String action = ""; @@ -53,23 +68,23 @@ public WritableMap processIntent() { if (type == null) { type = ""; } + if (Intent.ACTION_SEND.equals(action) && "text/plain".equals(type)) { - value = intent.getStringExtra(Intent.EXTRA_TEXT); - } - else if (Intent.ACTION_SEND.equals(action) && ("image/*".equals(type) || "image/jpeg".equals(type) || "image/png".equals(type) || "image/jpg".equals(type) ) ) { + text = intent.getStringExtra(Intent.EXTRA_TEXT); + } else if (Intent.ACTION_SEND.equals(action) && type.startsWith("image")) { Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); - value = "file://" + RealPathUtil.getRealPathFromURI(currentActivity, uri); - - } else { - value = ""; - } - } else { - value = ""; - type = ""; - } + images.pushString("file://" + RealPathUtil.getRealPathFromURI(currentActivity, uri)); + } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type.startsWith("image")) { + ArrayList uris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); + for (Uri uri : uris) { + images.pushString("file://" + RealPathUtil.getRealPathFromURI(currentActivity, uri)); + } + } + } map.putString("type", type); - map.putString("value",value); + map.putString("text", text); + map.putArray("images", images); return map; } diff --git a/android/src/main/java/com/alinz/parkerdan/shareextension/SharePackage.java b/android/src/main/java/com/alinz/parkerdan/shareextension/SharePackage.java index 23431dd5..3e168353 100644 --- a/android/src/main/java/com/alinz/parkerdan/shareextension/SharePackage.java +++ b/android/src/main/java/com/alinz/parkerdan/shareextension/SharePackage.java @@ -16,7 +16,6 @@ public List createNativeModules(ReactApplicationContext reactConte return Arrays.asList(new ShareModule(reactContext)); } - @Override public List> createJSModules() { return Collections.emptyList(); } diff --git a/android/src/main/java/com/github/alinz/reactNativeShareExtension/ShareExPackage.java b/android/src/main/java/com/github/alinz/reactNativeShareExtension/ShareExPackage.java index 84c591b2..f21656c3 100644 --- a/android/src/main/java/com/github/alinz/reactNativeShareExtension/ShareExPackage.java +++ b/android/src/main/java/com/github/alinz/reactNativeShareExtension/ShareExPackage.java @@ -17,7 +17,6 @@ public List createNativeModules(ReactApplicationContext reactConte return Arrays.asList(new ShareExModule(reactContext)); } - @Override public List> createJSModules() { return Collections.emptyList(); } diff --git a/lib/index.js b/lib/index.js index aa17dce1..941d7d3c 100644 --- a/lib/index.js +++ b/lib/index.js @@ -5,5 +5,6 @@ import { NativeModules } from 'react-native' // NativeModules.ShareExtension.close() export default { data: () => NativeModules.ReactNativeShareExtension.data(), - close: () => NativeModules.ReactNativeShareExtension.close() + close: () => NativeModules.ReactNativeShareExtension.close(), + clear: () => NativeModules.ReactNativeShareExtension.clear() }