Xamarin 在 Webview 中使用相机上传图片
Posted
技术标签:
【中文标题】Xamarin 在 Webview 中使用相机上传图片【英文标题】:Xamarin Upload pictures with camera in Webview 【发布时间】:2018-07-14 02:33:27 【问题描述】:我已经用 WebView 相机上传了图片,但是我想上传两张图片, 第一张图片上传了,第二张图片没有上传,为什么? 我需要做什么来解决这个问题? 注释是否需要代码的详细信息。 下面是我的代码
MyCustomwebviewRenderer.cs
Control.SetWebChromeClient(new WebViewChromeClient((uploadMsg, acceptType, capture) =>
MainActivity.UploadMessage = uploadMsg;
if (Build.VERSION.SdkInt < BuildVersionCodes.Kitkat)
var i = new Intent(Intent.ActionCameraButton);
//To set all type of files
i.SetType("image/*");
//Here File Chooser dialog is started as Activity, and it gives result while coming back from that Activity.
((MainActivity)this.Context).StartActivityForResult(Intent.CreateChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE);
else
File imgFile = new File("/sdcard/Pictures/Repair_Image.jpg");
mCapturedImageURI = android.Net.Uri.FromFile(new File(imgFile.ToString()));
if (mCapturedImageURI != null)
mCapturedImageURI.Dispose();
imgFile.Delete();
if (camera!=null)
camera.Release();
File imageStorageDir = new File(global::Android.OS.Environment.GetExternalStoragePublicDirectory(global::Android.OS.Environment.DirectoryPictures),"");
if (!imageStorageDir.Exists())
imageStorageDir.Mkdir();
File file = new File(imageStorageDir + File.Separator +"Repair_Image"+ ".jpg");
mCapturedImageURI = Uri.FromFile(file);
Intent captureIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);
captureIntent.PutExtra(MediaStore.ExtraOutput, mCapturedImageURI);
((MainActivity)this.Context).StartActivityForResult(captureIntent, MainActivity.FILECHOOSER_RESULTCODE);
));
还有我的 Mainactivity.cs
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
imgFile = new File("/sdcard/Pictures/Repair_Image.jpg");
mCapturedImageURI = Android.Net.Uri.FromFile(new File(imgFile.ToString()));
// Handles the response from the FileChooser
if (requestCode == FILECHOOSER_RESULTCODE && resultCode == Result.Ok)
if (null == UploadMessage)
return;
Java.Lang.Object result = intent == null || resultCode != Result.Ok ? mCapturedImageURI : intent.Data;
UploadMessage.OnReceiveValue(result);
UploadMessage = null;
// UploadMessage.OnReceiveValue(null);
【问题讨论】:
【参考方案1】:我想通了。我是这样写的:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
var file = getLatestFilefromDir("/sdcard/Pictures/CeptePortal/");
mCapturedImageURI = Android.Net.Uri.FromFile(file);
if (requestCode == FILECHOOSER_RESULTCODE && resultCode == Result.Ok)
if (null == UploadMessage)
return;
Android.Net.Uri result = null;
if (resultCode == Result.Ok)
result = intent == null ? mCapturedImageURI : intent.Data;
try
UploadMessage.OnReceiveValue(result);
UploadMessage = null;
catch (Exception ex)
throw;
private Java.IO.File getLatestFilefromDir(String dirPath)
File dir = new File(dirPath);
File[] files = dir.ListFiles();
if (files == null || files.Length == 0)
return null;
File lastModifiedFile = files[0];
for (int i = 1; i < files.Length; i++)
if (lastModifiedFile.LastModified() < files[i].LastModified())
lastModifiedFile = files[i];
return lastModifiedFile;
【讨论】:
我遇到了同样的问题,但需要查看您创建的其他课程 - 可以分享吗? 抱歉,我现在已经停止使用 Xamarin 进行开发并继续使用 Flutter。颤振是最好的!以上是关于Xamarin 在 Webview 中使用相机上传图片的主要内容,如果未能解决你的问题,请参考以下文章
在 Xamarin Android App 中使用 webview 上传文件
有没有办法让颤动的 webview 使用 android 相机进行文件上传?如何在 webview_flutter 中打开文件选择器?