java - Android Webview 应用程序,上传图片不起作用,点击选择照片时没有任何反应

标签 java android android-studio webview uploading

我已经创建了一个 Android Webview 应用程序并且它工作正常,但是当我点击用于上传照片的按钮时,没有任何反应。我附上我的代码,所以你们可以帮助我

主 Activity .java

public class MainActivity extends AppCompatActivity {
private WebView myWebView;


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

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    myWebView = new WebView(this);
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    myWebView.setWebViewClient(new WebViewClient());
    myWebView.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);
        }
    });

    this.setContentView(myWebView);

    myWebView.loadUrl("http://www.droidocial.com");
}

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;
    }
}

@Override
public void onBackPressed() {
    if (myWebView.canGoBack()) {
        myWebView.goBack();
    } else {
        super.onBackPressed();
    }
}

AndroidManifest.xml

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

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity android:name=".Splashscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="com.official.droidocial.droidocial.MAINACTIVITY" />

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

我尝试了太多,但仍然卡在这个错误中,谁能告诉我如何解决这个错误。

最佳答案

看起来这可以帮助 https://www.opengeeks.me/2015/08/filechooser-and-android-webview/ .

Uri[] results = null;
String dataString = intent.getDataString();
if (dataString != null) {
    results = new Uri[]{Uri.parse(dataString)};
}
mUploadMessage.onReceiveValue(results);

此外,您还需要此权限才能访问设备上的文件:

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

关于java - Android Webview 应用程序,上传图片不起作用,点击选择照片时没有任何反应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39186485/

相关文章:

java - 如何解析html并保留所有换行符?

java - 从 CSV raw 插入 SQLite 的更快方法?

java - Maven 模块层次结构

java - 如何解决错误找不到 Retention 和 IntDef 的符号类?

android - 在 Android Studio 中创建和测试库项目以在任何项目中共享的最佳方式是什么?

java - 如何使用drawString更改字符串中单个字符的颜色?

java - 给定两个排序列表(或数组)和一个数字 k,创建一个算法来获取两个列表中最少的 k 个数字

java - StAX createXMLEventReader 我应该使用 BufferedReader 吗?

java - 使用加密货币进行加密。加密失败,解密正常

android - 无法将 Android 应用程序上传到设备(陈旧的 dexed jar)