所以我有一个Android WebView,旨在导航m.facebook.com,我无法使图像上传工作。当我点击上传图片按钮(在我的WebView上的Facebook上)时没有任何反应。
我在m.facebook.com网站上按下的按钮就是这个:
我已按如下方式定义了我的WebChromeClient:
_webview.setWebChromeClient(new WebChromeClient() {
public void openFileChooser( ValueCallback<Uri> uploadMsg, String acceptType ) {
// BREAKPOINT HERE
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
MainActivity.this.startActivityForResult( Intent.createChooser( i, "" ), MainActivity.FILECHOOSER_RESULTCODE );
}
// For Android < 3.0
public void openFileChooser( ValueCallback<Uri> uploadMsg ) {
openFileChooser( uploadMsg, "" );
}
// For Android > 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture){
openFileChooser( uploadMsg, "" );
}
});
我从Chirag的回答中得到了以前的堆栈溢出帖子:Android WebView File Upload
我在这里设置了一个断点,在这里点了一小段时间&#34;注释是,即使按下m.facebook.com上的上传照片按钮,它也永远不会被击中。我有READ_EXTERNAL_STORAGE权限集。我的最小api版本是9,我的目标是21.
我还设置了一个WebViewClient,我定义如下:
private class MyWebViewClient extends WebViewClient {
Activity activity;
public MyWebViewClient(Activity myActivity){
activity = myActivity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
// This function is never called when I click the upload photo
// button.
// ... Do Stuff
}
public void onLoadResource(WebView view, String url){
// The following if statement's predicate returns false if the
// upload image button is pressed, so it's contents are of no consequence
if(url.startsWith(LINK_STRING)){
// ...
}
}
}
为了完整性,我已经包含了我的一部分清单:
<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version"/>
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:alwaysRetainTaskState="true"/> <!-- Remove this if this doesn't correct the crashing issue -->
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
我的问题是:如何允许WebView组件从图库和相机上传图像?
非常感谢您提供的任何帮助!
答案 0 :(得分:3)
您必须扩展WebChromeClient
并添加四种方法,如您所关联的问题中所述。
您可以使用此库为您执行以下操作:
https://github.com/delight-im/Android-AdvancedWebView
...或者查看源代码以了解它是如何完成的:
答案 1 :(得分:0)
添加以下onActivityResult来处理on filechooser。这些功能允许您请求文件选择器和对文件选择器执行的操作。 view demo
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE){
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null :intent.getData();
mUploadMessage.onReceiveValue(result); mUploadMessage = null;
}
}
您的MyWebViewClient应如下所示
private class MyWebViewClient extends WebViewClient {
Activity activity;
public MyWebViewClient(Activity myActivity){
activity = myActivity;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
// This function is never called when I click the upload photo
// button.
// ... Do Stuff
}
public void onLoadResource(WebView view, String url){
// The following if statement's predicate returns false if the
// upload image button is pressed, so it's contents are of no consequence
if(url.startsWith(LINK_STRING)){
// ...
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode==FILECHOOSER_RESULTCODE){
if (null == mUploadMessage)
return;
Uri result = intent == null || resultCode != RESULT_OK ? null :intent.getData();
mUploadMessage.onReceiveValue(result); mUploadMessage = null;
}
}
}
您可以从 here 下载演示同样的工作演示