日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 运维知识 > Android >内容正文

Android

android webview file,Android WebView 不支持 H5 input type=file 解决方法

發(fā)布時(shí)間:2023/12/2 Android 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android webview file,Android WebView 不支持 H5 input type=file 解决方法 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近因?yàn)橼s項(xiàng)目進(jìn)度,因此將本來(lái)要用原生控件實(shí)現(xiàn)的界面,自己做了H5并嵌入webview中。發(fā)現(xiàn)點(diǎn)擊H5中 input type="file" 標(biāo)簽 不能打開android資源管理器。

通過(guò)網(wǎng)絡(luò)搜索發(fā)現(xiàn)是因?yàn)?android webview 由于考慮安全原因屏蔽了 input type="file" 這個(gè)功能 。

經(jīng)過(guò)不懈的努力,以及google 翻譯的幫助 在 stackoverflow 中找到了解決的方法。

具體可以理解為 重寫webview 的WebChromeClient ,廢話不多說(shuō)直接貼代碼:

private ValueCallback mUploadMessage;

public ValueCallback uploadMessage;

public static final int REQUEST_SELECT_FILE = 100;

private final static int FILECHOOSER_RESULTCODE = 2;

webview.setWebChromeClient(new WebChromeClient(){

// For 3.0+ Devices (Start)

// onActivityResult attached before constructor

protected void openFileChooser(ValueCallback uploadMsg, String acceptType)

{

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("image/*");

startActivityForResult(Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE);

}

// For Lollipop 5.0+ Devices

@TargetApi(Build.VERSION_CODES.LOLLIPOP)

public boolean onShowFileChooser(WebView mWebView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)

{

if (uploadMessage != null) {

uploadMessage.onReceiveValue(null);

uploadMessage = null;

}

uploadMessage = filePathCallback;

Intent intent = fileChooserParams.createIntent();

try

{

startActivityForResult(intent, REQUEST_SELECT_FILE);

} catch (ActivityNotFoundException e)

{

uploadMessage = null;

Toast.makeText(getBaseContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();

return false;

}

return true;

}

//For Android 4.1 only

protected void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture)

{

mUploadMessage = uploadMsg;

Intent intent = new Intent(Intent.ACTION_GET_CONTENT);

intent.addCategory(Intent.CATEGORY_OPENABLE);

intent.setType("image/*");

startActivityForResult(Intent.createChooser(intent, "File Browser"), FILECHOOSER_RESULTCODE);

}

protected void openFileChooser(ValueCallback uploadMsg)

{

mUploadMessage = uploadMsg;

Intent i = new Intent(Intent.ACTION_GET_CONTENT);

i.addCategory(Intent.CATEGORY_OPENABLE);

i.setType("image/*");

startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);

}

});

}

@Override

public void onActivityResult(int requestCode, int resultCode, Intent intent)

{

if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)

{

if (requestCode == REQUEST_SELECT_FILE)

{

if (uploadMessage == null)

return;

uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));

uploadMessage = null;

}

}

else if (requestCode == FILECHOOSER_RESULTCODE)

{

if (null == mUploadMessage)

return;

// Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment

// Use RESULT_OK only if you're implementing WebView inside an Activity

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

mUploadMessage.onReceiveValue(result);

mUploadMessage = null;

}

else

Toast.makeText(getBaseContext(), "Failed to Upload Image", Toast.LENGTH_LONG).show();

}

以上所述是小編給大家介紹的Android WebView 不支持 H5 input type="file" 解決方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

總結(jié)

以上是生活随笔為你收集整理的android webview file,Android WebView 不支持 H5 input type=file 解决方法的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。