gpt4 book ai didi

android - webview,文件输入字段文件选择器不显示

转载 作者:行者123 更新时间:2023-11-30 00:42:11 25 4
gpt4 key购买 nike

我一直在努力尝试从 WebView 上传文件。我尝试了很多解决方案,但没有一个奏效。

http://m0s-programming.blogspot.tw/2011/02/file-upload-in-through-webview-on.html

Android WebView File Upload

当我单击按钮时,文件选择器在 chrome 等桌面浏览器中工作正常,浏览器会打开一个对话框,我可以在其中选择要上传的文件。

用手机测试程序点击选择文件对话框没有反应

有人可以帮助我吗?谢谢

最佳答案

引用此文档:

https://infeeds.com/d/CODEmgks/20475/upload-image-file-gallery-or-camera-webv

public boolean onShowFileChooser(
WebView webView, ValueCallback<Uri[]> filePathCallback,
WebChromeClient.FileChooserParams fileChooserParams){
if(mUMA != null){
mUMA.onReceiveValue(null);
}
mUMA = filePathCallback;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(takePictureIntent.resolveActivity(MainActivity.this.getPackageManager()) != null){
File photoFile = null;
try{
photoFile = createImageFile();
takePictureIntent.putExtra("PhotoPath", mCM);
}catch(IOException ex){
Log.e(TAG, "Image file creation failed", ex);
}
if(photoFile != null){
mCM = "file:" + photoFile.getAbsolutePath();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
}else{
takePictureIntent = null;
}
}
Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
contentSelectionIntent.setType("image/*");
Intent[] intentArray;
if(takePictureIntent != null){
intentArray = new Intent[]{takePictureIntent};
}else{
intentArray = new Intent[0];
}

Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooserIntent, FCR);
return true;
}

创建图像文件函数,如上面代码中提到的,我们需要这个来创建新的临时文件以上传。

private File createImageFile() throws IOException{
@SuppressLint("SimpleDateFormat") String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "img_"+timeStamp+"_";
File storageDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return File.createTempFile(imageFileName,".jpg",storageDir);
}

关于android - webview,文件输入字段文件选择器不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385063/

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