Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multi-images and content uri real path resolving #34

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public class MainApplication extends Application implements ReactApplication {
android:theme="@style/Theme.Share.Transparent" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<action android:name="android.intent.action.SEND_MULTIPLE" />
<category android:name="android.intent.category.DEFAULT" />
// for sharing links include
<data android:mimeType="text/plain" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand Down Expand Up @@ -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())) {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,6 +14,7 @@

import android.graphics.Bitmap;
import java.io.InputStream;
import java.util.ArrayList;


public class ShareModule extends ReactContextBaseJavaModule {
Expand All @@ -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();
Expand All @@ -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 = "";

Expand All @@ -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")) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are images the only things that can be "mutiple"? Is it not possible to send more than one thing of anther type?

Perhaps the shape of the object we return to RN is wrong. Maybe it should be an array of {type, value}? This way you could use it to share multiple contacts, image etc...

Copy link
Contributor

@arribbar arribbar Jul 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with you @npomfret. I think it would be better to return an array of {type, value}.
Also, it would be nice to be able to share other types as audio, video, etc.
I'll try to make a PR asap

ArrayList<Uri> 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;
}
Expand Down
3 changes: 2 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cbjs Can you explain the need for the addition of clear?

}