【问题标题】:File upload in web view not workingWeb 视图中的文件上传不起作用
【发布时间】:2016-10-06 02:19:00
【问题描述】:

我正在尝试通过网络视图上传文件,但目前没有发生,我正在查看this famous answer,这是我的

MainActivity.java

package ...;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    private ValueCallback<Uri> mUploadMessage;
    private final static int FILECHOOSER_RESULTCODE = 1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        WebView wv = (WebView) findViewById(R.id.webView);
        wv.getSettings().setJavaScriptEnabled(true);
        wv.setWebViewClient(new WebViewClient());
        wv.setWebChromeClient(new WebChromeClient() {
            //The undocumented magic method override
            //Eclipse will swear at you if you try to put @Override here
            public void openFileChooser(ValueCallback<Uri> uploadMsg) {
                MainActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 3.x
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
                MainActivity.this.showAttachmentDialog(uploadMsg);
            }

            // For Android > 4.1
            public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
                MainActivity.this.showAttachmentDialog(uploadMsg);
            }
        });

        wv.loadUrl("http://www.tizag.com/phpT/fileupload.php");

    }

    private void showAttachmentDialog(ValueCallback<Uri> uploadMsg) {
        this.mUploadMessage = uploadMsg;

        Intent i = new Intent(Intent.ACTION_GET_CONTENT);
        i.addCategory(Intent.CATEGORY_OPENABLE);
        i.setType("*/*");

        this.startActivityForResult(Intent.createChooser(i, "Choose type of attachment"), FILECHOOSER_RESULTCODE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == FILECHOOSER_RESULTCODE) {
            if (null == this.mUploadMessage) {
                return;
            }
            Uri result = intent == null || resultCode != RESULT_OK ? null : intent.getData();
            this.mUploadMessage.onReceiveValue(result);
            this.mUploadMessage = null;
        }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="....MainActivity">

    <WebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/webView"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

和权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="...">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />


</manifest>

如果您设置这 3 个文件并运行它,我已将 webview url 设置为包含文件输入的页面,您会发现它不起作用。我正在使用 API 版本 19+

我快要哭了。

【问题讨论】:

  • 我忘了什么时候了,但 Android 团队在 4.x 之后的某个时间删除了未记录的 openFileChooser 方法。我们让它普遍工作的方式是实现一个 JavaBridge 来调用文件选择器,并通过服务上传文件。然后表单会收到一个 UUID,服务器可以使用它来解析上传的文件。它很复杂,但无论 SDK 还是设备的 WebView 都能正常工作。
  • @323go 好,有什么资源可以帮我完成这个过程吗?
  • 所有的东西都在那里,但你必须自己把它们粘在一起。哦,为了将 GUID 注入表单,我们在 loadUrl 调用中使用了 javascript。通过 Android 的 Activity pause/resume 使表单生效是另一组问题——导航到 fileChooser 可能会重置您的表单数据,除非您将其持久化。可以说,这不是微不足道的。将客户端代码放在一起并删除专有位将花费我一段时间,而我只是没有时间。此外,我什至不知道我是否仍然可以访问服务器端代码。

标签: android file webview upload


【解决方案1】:

所以我发现了这个我认为会有所帮助的 GIT 存储库 - https://github.com/GoogleChrome/chromium-webview-samples

查看“输入文件示例”。

我遇到了 Build.Gradle 问题,并在这里找到了解决方案 - Gradle DSL method not found: 'runProguard'

希望这会有所帮助!

【讨论】:

  • 虽然这些链接可能会回答问题,但最好在此处包含答案的基本部分并提供链接以供参考。如果链接页面发生更改,仅链接的答案可能会失效。
猜你喜欢
  • 1970-01-01
  • 2017-03-09
  • 1970-01-01
  • 2022-11-23
  • 2017-12-12
  • 1970-01-01
  • 2018-02-07
  • 1970-01-01
  • 2010-12-27
相关资源
最近更新 更多