android - 如何通过 ACTION_SEND Intent 将文件上传到 webview

标签 android android-intent webview

我正在尝试通过 ACTION_SEND Intent 将文件上传到 WebView 应用程序。如果他们选择在应用程序内部使用 openFileChooser 上传文件,我已经让它工作,但当他们从文档或图库应用程序单击共享时,我需要它工作。所以,我非常需要绕过 openFileChooser,因为已经选择了一个文件并且它的 Uri 在我的 Intent 中,我只需要实际上传它。我想借鉴 openFileChooser 的做法,但我不确定该怎么做。

我认为我最大的障碍是不知道如何获取/创建/忽略 ValueCallback uploadMsg,当然,除非有一种方法可以完全避免这种情况。

这是我的 Intent 和 openFileChooser,但我需要在收到 Intent 时绕过它,所以我有

intent = getActivity().getIntent();
    if(intent != null){
        intentAction = intent.getAction();
        if(!TextUtils.isEmpty(intentAction)){
            if(intentAction.equalsIgnoreCase(Intent.ACTION_SEND)){
                if(intent.getExtras()!=null){
                   //get file from extra and piggyback off upload handler used in openFileChooser
                }
            }
        }

我的 openFileChooser 可以正常工作,有用户输入,但我需要能够在没有用户输入的情况下接收到 Intent

WebChromeClient chromeClient = new WebChromeClient() {
        @Override
        public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType,
                                    String capture) {
                mUploadHandler = new UploadHandler(WebFragment.this);
                mUploadHandler.openFileChooser(uploadMsg, acceptType, capture);
        }

所以我需要以某种方式获得一个有效的 ValueCallback Uri 以设置为创建 openFileChooser 的上传消息,这样当我在这里执行 mUploadMessage.onReceiveValue(result)(取自 google 的 webkit 包)时

void onResult(int resultCode, Intent intent) {

    if (resultCode == Activity.RESULT_CANCELED && mCaughtActivityNotFoundException) {
        mCaughtActivityNotFoundException = false;
        return;
    }

    Uri result = intent == null || resultCode != Activity.RESULT_OK ? null
            : intent.getData();
    if (result == null && intent == null && resultCode == Activity.RESULT_OK) {
        File cameraFile = new File(mCameraFilePath);
        if (cameraFile.exists()) {
            result = Uri.fromFile(cameraFile);
            mController.getActivity().sendBroadcast(
                    new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result));
        }
    }

    mUploadMessage.onReceiveValue(result);
    mHandled = true;
    mCaughtActivityNotFoundException = false;
}

所以,长话短说,我需要一个有效的 ValueCallback<Uri>为了搭载 Android/webkit 的 onReceiveValue(result)/WebView 的上传处理程序,我不确定如何在没有用户输入的情况下从设备打开上传。

最佳答案

正如对您问题的第一条评论所说,更多代码将有助于对此进行诊断,但接收 ACTION_SEND 的通用方法 Intent 如下。

配置ACTION_SEND作为Intent Filter在您的 Activity 的 Android list 中。

<application>
...
    <activity>
    ...
    <intent-filter>
        <action android:name="android.intent.action.SEND" />    
        <category android:name="android.intent.category.DEFAULT" />   
    </intent-filter>

您可以选择设置多个 <data android:mimeType=XXX>元素,如果您只想接受某些类型的文件。

然后在您的 Activity 中,您可以使用 getIntent()Intent.getData()处理传入发送 Intent 的方法。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    Uri sharedFile = intent.getData();
    // We can now use the URI of the file / item in
    // the WebView. Probably want to do this after onCreate
    // so that the WebView has fully initialised.
}

关于android - 如何通过 ACTION_SEND Intent 将文件上传到 webview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29503611/

相关文章:

java - 自定义 Facebook 登录以与 MethodChannel 一起使用

安卓 : WebView onPageFinished not calling after shouldOverrideUrlLoading method in android 4. 4

android - Webview 在注销期间抛出 ERR_NAME_NOT_RESOLVED

android - NFC 远距离读取 RFID 标签

android - 在 Recyclerview 中拖动项目时自动滚动滚动条

android - 使用 ContentProvider 从联系人获取电话号码 - Android

javascript - 是否可以在页面加载之前将JS注入(inject)Android WebView?

android - 没有用户 Intent 的 Chrome 自定义标签

java - 如何每 5 分钟唤醒一次我的 Intent 服务

android - Camera Intent 未返回调用 Activity