Android上的React Native Share文件不起作用

时间:2018-06-08 03:20:46

标签: android react-native

反应天然-0.55.4 我下载一个文件,然后分享它,就像这样:

RNFetchBlob
  .config({
    fileCache: true,
    appendExt: 'jpg',
    overwrite: true,
    indicator: true
  })
  .fetch('GET', 'http://xxx.jpg')
  .then((response) => {
    console.log(response)
    const filePath = response.path()
    const fileUrl = `file://${filePath}`
    Share.share({url: fileUrl})
      .then((result) => {
        alert(result)
      })
      .catch((e) => {
        alert(JSON.stringify(e))
      })
    Share.dismissedAction = (info) => {
      alert(JSON.stringify(info))
    }
  })
  .catch((e) => {
    console.log(e)
  })

应用程序面板显示 image

按其中一个文件没有发送,所以我查了找到的本机代码ShareModule.java:

  public void share(ReadableMap content, String dialogTitle, Promise promise) {
    if (content == null) {
      promise.reject(ERROR_INVALID_CONTENT, "Content cannot be null");
      return;
    }

    try {
      Intent intent = new Intent(Intent.ACTION_SEND);
      intent.setTypeAndNormalize("text/plain");

      if (content.hasKey("title")) {
        intent.putExtra(Intent.EXTRA_SUBJECT, content.getString("title"));
      }

      if (content.hasKey("message")) {
        intent.putExtra(Intent.EXTRA_TEXT, content.getString("message"));
      }

      Intent chooser = Intent.createChooser(intent, dialogTitle);
      chooser.addCategory(Intent.CATEGORY_DEFAULT);

      Activity currentActivity = getCurrentActivity();
      if (currentActivity != null) {
        currentActivity.startActivity(chooser);
      } else {
        getReactApplicationContext().startActivity(chooser);
      }
      WritableMap result = Arguments.createMap();
      result.putString("action", ACTION_SHARED);
      promise.resolve(result);
    } catch (Exception e) {
      promise.reject(ERROR_UNABLE_TO_OPEN_DIALOG, "Failed to open share dialog");
    }
  }

众所周知,在Android 7.0共享文件之后应该像这样https://developer.android.com/training/secure-file-sharing/share-file 所以我改变了一些代码并使用我自己的桥:

  public void share(String filePathToShare, Promise promise){
    File file = new File(filePathToShare);
    if(file.exists()) {
       Uri fileUri = FileProvider.getUriForFile(getCurrentActivity(), "yourapplication.provider", file);
       Intent intentShareFile = new Intent(Intent.ACTION_SEND);
       intentShareFile.setDataAndType(fileUri, 
          getCurrentActivity().getContentResolver().getType(fileUri));
          intentShareFile.putExtra(Intent.EXTRA_STREAM, fileUri);
       getCurrentActivity().startActivity(Intent.createChooser(intentShareFile, "Share File"));
    }
}

它适用于我,所以还有另一种方法来共享文件只需使用react-native api而不更改源代码?或者,它会支持新版本吗?

0 个答案:

没有答案