选择图像时Android Webview重复文件
Posted
技术标签:
【中文标题】选择图像时Android Webview重复文件【英文标题】:Android Webview Duplicate Files when Selecting Image 【发布时间】:2021-11-20 03:39:34 【问题描述】:我在 android webview 上上传文件时遇到问题,当我选择要选择的文件或相机时,会形成一个 CAPUTURE_OOOOOOOO.jpg 文件,该文件只有在我设法拍照并拍摄图像时才会形成进入选定的图像文件,这里是 kotlin 的代码
myWebView.webChromeClient = object : WebChromeClient()
override fun onShowFileChooser(webView: WebView, filePathCallback: ValueCallback<Array<Uri>>, fileChooserParams: FileChooserParams):Boolean
chooserCallback = filePathCallback
val fileCamera = File(
Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES
), "CAPTURE_" + System.currentTimeMillis().toString() + ".jpg"
)
chooserCamera = Uri.fromFile(fileCamera)
val captureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, chooserCamera)
val imageIntent = Intent(Intent.ACTION_GET_CONTENT)
imageIntent.addCategory(Intent.CATEGORY_OPENABLE)
imageIntent.type = "image/*"
val chooserIntent = Intent.createChooser(imageIntent, "Select Image")
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, arrayOf(captureIntent))
startActivityForResult(chooserIntent, requestChooser)
return true
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?)
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == requestChooser)
if (resultCode == Activity.RESULT_OK)
Log.v("chooserCamera", data?.dataString.toString())
Log.v("dataString", data?.dataString.toString())
val result: Uri = if (data?.dataString == null) chooserCamera as Uri else data.data as Uri
chooserCallback?.onReceiveValue(arrayOf(result))
chooserCallback = null
if (resultCode == Activity.RESULT_CANCELED)
Toast.makeText(applicationContext, "No Image", Toast.LENGTH_SHORT).show()
chooserCallback?.onReceiveValue(null)
chooserCallback = null
CAPUTURE_OOOOOOOO.jpg文件应该只在我用相机拍照并选择时形成,而不是在我按下选择按钮时形成,因此在此过程中存在文件重复,谢谢
从虚拟设备录制的视频 - Nexus 5 Api 30:https://streamable.com/0kylsu
【问题讨论】:
您的代码中没有任何内容可以创建该文件。此外,使用的相机应用程序也无法创建该文件,因为 Uri.fromFile() 自 Android 7/N 以来不可用。 进一步不清楚为什么你在拍照时会乱用 ACTION_GET_CONTENT。 @blackapps 你可以看这个视频streamable.com/0kylsu @adyoi 如果您希望得到帮助,您可以考虑花额外的时间将所有相关信息提取到您的问题中,而不是提示潜在的有帮助的人观看视频。 . 【参考方案1】: 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
public boolean onShowFileChooser(WebView mWebView, ValueCallback<Uri[]> 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(getActivity().getApplicationContext(), "Cannot Open File Chooser", Toast.LENGTH_LONG).show();
return false;
return true;
//For Android 4.1 only
protected void openFileChooser(ValueCallback<Uri> 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<Uri> 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);
);
【讨论】:
你能用kotlin吗以上是关于选择图像时Android Webview重复文件的主要内容,如果未能解决你的问题,请参考以下文章