Android中的webview点击上传控件无效
Android中的webview点击上传控件无效
下雨天_冒泡泡 发表于1年前
Android中的webview点击上传控件无效
  • 发表于 1年前
  • 阅读 49
  • 收藏 0
  • 点赞 0
  • 评论 0

移动开发云端新模式探索实践 >>>   

https://zhuanlan.zhihu.com/p/21643401

public class MainActivity extends AppCompatActivity {

private ValueCallback<Uri> uploadMessage;

private ValueCallback<Uri[]> uploadMessageAboveL;

private final static int FILE_CHOOSER_RESULT_CODE = 10000;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

WebView webview = (WebView) findViewById(R.id.web_view);

assert webview != null;

WebSettings settings = webview.getSettings();

settings.setUseWideViewPort(true);

settings.setLoadWithOverviewMode(true);

settings.setJavaScriptEnabled(true);

webview.setWebChromeClient(new WebChromeClient() {

// For Android < 3.0

public void openFileChooser(ValueCallback<Uri> valueCallback) {

uploadMessage = valueCallback; openImageChooserActivity();

}

// For Android >= 3.0

public void openFileChooser(ValueCallback valueCallback, String acceptType) {

uploadMessage = valueCallback; openImageChooserActivity();

}

//For Android >= 4.1

public void openFileChooser(ValueCallback<Uri> valueCallback, String acceptType, String capture) {

uploadMessage = valueCallback;

openImageChooserActivity();

}

// For Android >= 5.0

@Override

public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {

uploadMessageAboveL = filePathCallback;

openImageChooserActivity();

return true;

}

});

String targetUrl = "file:///android_asset/up.html";

webview.loadUrl(targetUrl);

}

private void openImageChooserActivity() {

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("image/*");

startActivityForResult(Intent.createChooser(i, "Image Chooser"), FILE_CHOOSER_RESULT_CODE);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode == FILE_CHOOSER_RESULT_CODE) {

if (null == uploadMessage && null == uploadMessageAboveL)

return;

Uri result = data == null || resultCode != RESULT_OK ? null : data.getData();

if (uploadMessageAboveL != null) {

onActivityResultAboveL(requestCode, resultCode, data);

} else if (uploadMessage != null) {

uploadMessage.onReceiveValue(result);

uploadMessage = null;

           }

    }

}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

private void onActivityResultAboveL(int requestCode, int resultCode, Intent intent) {

if (requestCode != FILE_CHOOSER_RESULT_CODE || uploadMessageAboveL == null)

return;

Uri[] results = null;

if (resultCode == Activity.RESULT_OK) {

if (intent != null) {

String dataString = intent.getDataString();

ClipData clipData = intent.getClipData();

if (clipData != null) {

results = new Uri[clipData.getItemCount()];

for (int i = 0; i < clipData.getItemCount(); i++) {

ClipData.Item item = clipData.getItemAt(i);

results[i] = item.getUri();

    }

} if (dataString != null)

results = new Uri[]{Uri.parse(dataString)};

     }

}

uploadMessageAboveL.onReceiveValue(results);

uploadMessageAboveL = null;

    }

}

  • 打赏
  • 点赞
  • 收藏
  • 分享
共有 人打赏支持
粉丝 3
博文 24
码字总数 11422
×
下雨天_冒泡泡
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: