alinz / react-native-share-extension

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

Audio and Video share empty value problem #199

Open mnuriyumusak opened 4 years ago

mnuriyumusak commented 4 years ago

When I try to share ogg, mp3 or mpeg file type comes correct but value is empty. Why value is empty. It is not happening when I share text or image files.

Pzz0912 commented 4 years ago

Same, have you solved it?

mnuriyumusak commented 4 years ago

Same, have you solved it?

I have solved just the "sharing ogg file" part. However, it is very specific solution. I edit the code actually I have added new lines to the this library for my need. I do not know if my solution suits you or not? Normally this library returns the uri of the file, however, my code returns bytes of the voice file that is shared.

flo-wolf commented 4 years ago

Same, have you solved it?

I have solved just the "sharing ogg file" part. However, it is very specific solution. I edit the code actually I have added new lines to the this library for my need. I do not know if my solution suits you or not? Normally this library returns the uri of the file, however, my code returns bytes of the voice file that is shared.

Would you be so kind and share your solution?

mnuriyumusak commented 4 years ago

Same, have you solved it?

I have solved just the "sharing ogg file" part. However, it is very specific solution. I edit the code actually I have added new lines to the this library for my need. I do not know if my solution suits you or not? Normally this library returns the uri of the file, however, my code returns bytes of the voice file that is shared.

Would you be so kind and share your solution?

node_modulers -> react-native-share-extension -> android -> src -> main -> java -> com -> alinz -> parkerdan -> shareextension in this path my ShareModule.java class is below .

In this way, when user shares an ogg voice file, I can get the bytes of the file.

package com.alinz.parkerdan.shareextension;

import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.Arguments;

import android.app.Activity;
import android.app.Application;
import android.content.Intent;
import android.net.Uri;

import android.graphics.Bitmap;
import android.util.Log;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.util.Base64;
import android.content.Context;
import android.content.ContentResolver;

public class ShareModule extends ReactContextBaseJavaModule {

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

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

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

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

  public byte[] getBytes(InputStream inputStream) throws IOException {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        int len = 0;
        while ((len = inputStream.read(buffer)) != -1) {
            byteBuffer.write(buffer, 0, len);
        }
        return byteBuffer.toByteArray();
    }

  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("typeeeeeee","e"+type);
        if (type == null) {
          type = "";
        }
       if(Intent.ACTION_SEND.equals(action) && "audio/ogg; codecs=opus".equals(type) || "audio/ogg".equals(type)){
           Uri uri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
            InputStream iStream = null;
            try {
                iStream = currentActivity.getBaseContext().getContentResolver().openInputStream(uri);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            byte[] inputData = new byte[0];
            try {
                inputData = getBytes(iStream);
            } catch (IOException e) {
                e.printStackTrace();
            }
            value = Base64.encodeToString(inputData, Base64.DEFAULT);
       }
       else {
         value = "";
       }
      } else {
        value = "";
        type = "";
      }

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

      return map;
  }
}
ajith-ab commented 4 years ago

i ve used this package with all new features with react native greater than 0.60 support react-native-receive-sharing-intent