不工作-6ren"> 不工作-我在网站上保存了一个 html 文件。 https://pinapakait.com/cam/testcam.html 它有以下简单的输入类型文件代码input type="file"accept="-6ren">
gpt4 book ai didi

Android WebView <输入类型 ="file"> 不工作

转载 作者:行者123 更新时间:2023-12-04 23:59:39 25 4
gpt4 key购买 nike

我在网站上保存了一个 html 文件。

https://pinapakait.com/cam/testcam.html

它有以下简单的输入类型文件代码input type="file"accept="image/*;capture=camera"

当通过网络浏览器在移动设备中打开时,它正在打开文件、相机选项。但是现在在 android 的 web View 中打开。

非常感谢您的帮助。

授予以下权限

uses-permission android:name="android.permission.INTERNET" 
uses-permission android:name="android.permission.CAMERA"
uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
uses-feature android:name="android.hardware.camera" android:required="true"
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"

uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"
uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"

为 WebView 设置以下设置。

    setLoadsImagesAutomatically(true)
setJavaScriptEnabled(true)
setDomStorageEnabled(true)
setLoadWithOverviewMode(true)
setPluginState(WebSettings.PluginState.ON)
setMediaPlaybackRequiresUserGesture(false)
setPluginState(WebSettings.PluginState.ON)
setAppCacheEnabled(true)

setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY)
setAllowFileAccessFromFileURLs(true)
setAllowUniversalAccessFromFileURLs(true)
setJavaScriptCanOpenWindowsAutomatically(true)
setBuiltInZoomControls(true)

setPluginsEnabled(true)
setAllowFileAccess(true)
setAllowContentAccess(true)
setSupportZoom(true)

最佳答案

我在 webview 上遇到了同样的问题,在谷歌搜索后,我通过扩展 WebChromeClient 找到了这个解决方案。

    public class MyWebChromeClient extends WebChromeClient {

DetailsFragmentWebView context;
public ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> mUploadMessageAboveL;
public Uri mCapturedImageURI;

public MyWebChromeClient(DetailsFragmentWebView context){
this.context = context;
//this.mCapturedImageURI = mCapturedImageURI;
//this.mUploadMessage = mUploadMessage;
//this.mUploadMessageAboveL = mUploadMessageAboveL;
}

// openFileChooser for Android 3.0+
public void openFileChooser(ValueCallback<Uri> uploadMsg, ValueCallback<Uri[]> uploadMsgAboveL, String acceptType){

// Update message
mUploadMessage = uploadMsg;
mUploadMessageAboveL = uploadMsgAboveL;
try{

// Create AndroidExampleFolder at sdcard

File imageStorageDir = new File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
, "AndroidExampleFolder");

if (!imageStorageDir.exists()) {
// Create AndroidExampleFolder at sdcard
imageStorageDir.mkdirs();
}

// Create camera captured image file path and name
File file = new File(
imageStorageDir + File.separator + "IMG_"
+ String.valueOf(System.currentTimeMillis())
+ ".jpg");

mCapturedImageURI = Uri.fromFile(file);

// Camera capture image intent
final Intent captureIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);

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

// Create file chooser intent
Intent chooserIntent = Intent.createChooser(i, "Image Chooser");

// Set camera intent to file chooser
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS
, new Parcelable[] { captureIntent });

// On select image call onActivityResult method of activity
context.startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);

}
catch(Exception e){
Toast.makeText(context.getActivity(), "Chrome Exception:"+e,
Toast.LENGTH_LONG).show();
}

}


//openFileChooser for other Android versions
public void openFileChooser(ValueCallback<Uri> uploadMsg,
String acceptType,
String capture) {

openFileChooser(uploadMsg , mUploadMessageAboveL, acceptType);
}

@Override
public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
openFileChooser(null ,filePathCallback , "");
return true;
}
}

然后将您的 webview 设置为使用此类:

MyWebChromeClient  myChromeClient = new myChromeClient(this);
webView.webChromeClient = myChromeClient;

您还需要将此代码添加到您的 Activity 中:

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
//super.onActivityResult(requestCode, resultCode, data)
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == myChromeClient.mUploadMessage && null == myChromeClient.mUploadMessageAboveL) return
val result = if (resultCode != RESULT_OK) null
else if (data!=null) data.data
else myChromeClient.mCapturedImageURI
if (myChromeClient.mUploadMessageAboveL != null) {
onActivityResultAboveL(requestCode, resultCode, data)
} else if (myChromeClient.mUploadMessage != null) {
myChromeClient.mUploadMessage?.onReceiveValue(result)
myChromeClient.mUploadMessage = null
}
}
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private fun onActivityResultAboveL(requestCode: Int, resultCode: Int, intent: Intent?) {
if (requestCode != FILECHOOSER_RESULTCODE || myChromeClient.mUploadMessageAboveL == null)
return
var results: Array<Uri>? = null
if (resultCode == Activity.RESULT_OK) {
if (intent != null) {
val dataString = intent.dataString
val clipData = intent.clipData
if (clipData != null) {
results = Array<Uri>(clipData.itemCount) { _->return Unit}
for (i in 0 until clipData.itemCount) {
val item = clipData.getItemAt(i)
results[i] = item.uri
}
}
if (dataString != null)
results = arrayOf(Uri.parse(dataString))
}else if (myChromeClient.mCapturedImageURI!=null)
results = arrayOf(myChromeClient.mCapturedImageURI)
}
myChromeClient.mUploadMessageAboveL?.onReceiveValue(results)
myChromeClient.mUploadMessageAboveL = null
}

关于Android WebView <输入类型 ="file"> 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62619834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com