alinz / react-native-share-extension

react-native as an engine to drive share extension
MIT License
763 stars 398 forks source link

Sharing not working, from google photos Android #93

Closed aashikhameed closed 6 years ago

aashikhameed commented 6 years ago

When i tried to share an image in ANDROID from the google photos app, (image not in the local storage) ,ShareExtension.data(); returns null value. Can anyone please let me know how to fix this.

BigPun86 commented 6 years ago

How did you fix it @aashikhameed?

aashikhameed commented 6 years ago

@BigPun86 , I have modified the Share module inside alinz.parkerdan.shareextension as follows,

public class ShareModule extends ReactContextBaseJavaModule {

ReactApplicationContext reactApplicationContext;

public ShareModule(ReactApplicationContext reactContext) {
    super(reactContext);
    reactApplicationContext = reactContext;
}

@Override
public String getName() {
    return "ReactNativeShareExtension";
}

@ReactMethod
public void close() {
    getCurrentActivity().finish();
}

@ReactMethod
public void data(Promise promise) {
    promise.resolve(processIntent());
}

public WritableMap processIntent() {
    WritableMap map = Arguments.createMap();

    String value = "";
    String type = "";
    String action = "";

    Activity currentActivity = getCurrentActivity();

    if (currentActivity != null) {
        Intent intent = currentActivity.getIntent();
        action = intent.getAction();
        type = intent.getType();
        Log.e("FILE TYPE : ", type);
        Log.e("FILE PATH : ", (((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM)).getPath()));
        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))) {
            Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            if (uri.toString().startsWith("content://")) {
                try {
                    value = "file://" + convertToBitmapAndCreateAFile((Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM));
                } catch (IOException e) {
                    value = "";
                    e.printStackTrace();
                }
            } else {
                value = "file://" + RealPathUtil.getRealPathFromURI(currentActivity, uri);
            }

        } else {
            value = "";
        }
    } else {
        value = "";
        type = "";
    }

    map.putString("type", type);
    map.putString("value", value);

    return map;
}

/**
 * Edited by : AASHIK HAMEED :  Convert the Uri content to bitmap and reduce the quality of the image then save it inside a cache file.
 * @param Uri
 * @return
 * @throws IOException
 */
private String convertToBitmapAndCreateAFile(Uri Uri) throws IOException {
    Bitmap bitmap = MediaStore.Images.Media.getBitmap(reactApplicationContext.getContentResolver(), Uri);

    //create a file to write bitmap data
    File f = new File(reactApplicationContext.getCacheDir(), "shareImage_ "+ System.currentTimeMillis() +".jpeg");
    f.createNewFile();
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bos);
    byte[] bitmapdata = bos.toByteArray();

    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();

    return f.getAbsolutePath();
}

}