Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Android Camera WebView Image Upload ERR_FILE_NOT_FOUND

$
0
0

I am working on a simple app that views a webpage that has an area for a user to take pictures and upload them to the site.
I found some examples that look like this

 public class CustomWebViewRenderer : WebViewRenderer
    {
        Activity mContext;
        private static int FILECHOOSER_RESULTCODE = 1;
        public CustomWebViewRenderer(Context context) : base(context)
        {
            mContext = context as Activity;
        }
        protected override void OnElementChanged(ElementChangedEventArgs<WebView> e)
        {
            try
            {
                base.OnElementChanged(e);

                var chromeClient = new FileChooserWebChromeClient((uploadMsg, acceptType, capture) => {
                    MainActivity.UploadMessage = uploadMsg;

                    // Create MyAppFolder at SD card for saving our images
                    File imageStorageDir = new File(Android.OS.Environment.GetExternalStoragePublicDirectory(
                            Android.OS.Environment.DirectoryPictures), "MyAppFolder");

                    if (!imageStorageDir.Exists())
                    {
                        imageStorageDir.Mkdirs();
                    }

                    // Create camera captured image file path and name, add ticks to make it unique 
                    var file = new File(imageStorageDir + File.Separator + "IMG_"
                        + DateTime.Now.Ticks + ".jpg");

                    MainActivity.mCapturedImageURI = Android.Net.Uri.FromFile(file);

                    // Create camera capture image intent and add it to the chooser
                    var captureIntent = new Intent(MediaStore.ActionImageCapture);
                    captureIntent.PutExtra(MediaStore.ExtraOutput, MainActivity.mCapturedImageURI);

                    var i = new Intent(Intent.ActionGetContent);
                    i.AddCategory(Intent.CategoryOpenable);
                    i.SetType("image/*");

                    var chooserIntent = Intent.CreateChooser(i, "Choose image");
                    chooserIntent.PutExtra(Intent.ExtraInitialIntents, new Intent[] { captureIntent });

                    (mContext).StartActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
                });

                Control.SetWebChromeClient(chromeClient);
            }
            catch (Exception ex)
            {
                var j = ex;                
            }            
        }
    }

 public class FileChooserWebChromeClient : WebChromeClient
    {
        private Action<IValueCallback, Java.Lang.String, Java.Lang.String> callback;

        public FileChooserWebChromeClient(Action<IValueCallback, Java.Lang.String, Java.Lang.String> callBack)
        {
            callback = callBack;
        }

        // For Android < 5.0
        [Java.Interop.Export]
        public void openFileChooser(IValueCallback uploadMsg, Java.Lang.String acceptType, Java.Lang.String capture)
        {
            callback(uploadMsg, acceptType, capture);
        }

        // For Android > 5.0
        public override Boolean OnShowFileChooser(Android.Webkit.WebView webView, IValueCallback uploadMsg, WebChromeClient.FileChooserParams fileChooserParams)
        {
            try
            {
                callback(uploadMsg, null, null);
            }
            catch (Exception ex)
            {
                var j = ex;
            }
            return true;
        }
    }

Added this to the MainActivity

public static Android.Webkit.IValueCallback UploadMessage;
private static int FILECHOOSER_RESULTCODE = 1;
public static Android.Net.Uri mCapturedImageURI;
protected override void OnActivityResult(int requestCode, Result resultCode, Android.Content.Intent data)
        {
            if (requestCode == FILECHOOSER_RESULTCODE)
            {
                if (null == UploadMessage)
                    return;

                Java.Lang.Object result;

                if (data != null && data.Data != null)
                    result = data.Data;
                else
                    result = mCapturedImageURI;

                UploadMessage.OnReceiveValue(new Android.Net.Uri[] { (Android.Net.Uri)result });
                UploadMessage = null;
            }
            else
                base.OnActivityResult(requestCode, resultCode, data);
        }

This works fine on older versions of Android but when I loaded it up on Android 8, I get the message "ERR_FILE_NOT_FOUND" from the website.
I feel like I'm close, but missing something small to make this work.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>