locked
unable to take photo / record video with camera and upload it. RRS feed

  • Question

  • User385718 posted

    Hi,

    I have an webviewbased android app and in my app I need to take photo's and record videos with the camera. When I take a photo and record a video it all goes well. But when I want to upload it the app crashes. This is my error:

    If I pick a file upload it it all works well. I just can't take a photo / record video and directly upload it.

    Does anyone how to resolve this?

    Wednesday, October 23, 2019 7:31 AM

All replies

  • User382871 posted

    Try the code. ``` 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;
    

    } ```

    Similar issue: https://forums.xamarin.com/discussion/120599/upload-pictures-with-camera-in-webview

    Wednesday, October 23, 2019 3:53 PM
  • User385718 posted

    Hi Jarvan,

    Thank you for your response. Unfortunately you're method getLatestFilefromDir did not help.

    on this line: File[] files = dir.ListFiles();

    files is null en will always return null in the if statement.

    Im looking forward to your response.

    Friday, October 25, 2019 6:51 AM
  • User382871 posted

    files is null en will always return null in the if statement. The path may be wrong, use GetFolderPath method to get the pictures path. ``` var path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures); var file = getLatestFilefromDir(path ); ...

    private Java.IO.File getLatestFilefromDir(String dirPath) { // } ```

    Friday, October 25, 2019 9:41 AM
  • User385718 posted

    Hi Jarvan,

    Unfortunately the files list still returns null.

    Maybe I am doing something wrong in my OnShowFileChooser method.

    here is my method i hope this helps:

    public override bool OnShowFileChooser(Android.Webkit.WebView webView, IValueCallback uploadMsg, WebChromeClient.FileChooserParams fileChooserParams) { MainActivity.mUploadMessage = uploadMsg;

            string[] type = fileChooserParams.GetAcceptTypes();
    
            if (type[0].Equals("image/*"))
            {
                File imageStorageDir = new File(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyPictures));
    
                if (!imageStorageDir.Exists())
                {
                    imageStorageDir.Mkdirs();
                }
    
                var file = new File(imageStorageDir + File.Separator + "IMG_"
                    + DateTime.Now.Ticks + ".jpg");
    
                MainActivity.mCapturedImageURI = Android.Net.Uri.FromFile(file);
    
                var captureImageIntent = new Intent(MediaStore.ActionImageCapture);
                captureImageIntent.PutExtra(MediaStore.ExtraOutput, MainActivity.mCapturedImageURI);
    
                Intent 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[] { captureImageIntent });
    
    
                ((Activity)Forms.Context).StartActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE);
            }
            else
            {
                File videoStorageDir = new File(global::Android.OS.Environment.GetExternalStoragePublicDirectory(
                    global::Android.OS.Environment.DirectoryMovies), "");
    
                if (!videoStorageDir.Exists())
                {
                    videoStorageDir.Mkdirs();
                }
    
                var file = new File(videoStorageDir + File.Separator + "VID_"
                    + DateTime.Now.Ticks + ".mp4");
    
                MainActivity.mCapturedImageURI = Android.Net.Uri.FromFile(file);
    
                var captureVideoIntent = new Intent(MediaStore.ActionVideoCapture);
                captureVideoIntent.PutExtra(MediaStore.ExtraOutput, MainActivity.mCapturedImageURI);
    
                Intent n = new Intent(Intent.ActionGetContent);
                n.AddCategory(Intent.CategoryOpenable);
                n.SetType("video/*");
    
                var chooserIntent = Intent.CreateChooser(n, "Choose video");
                chooserIntent.PutExtra(Intent.ExtraInitialIntents, new Intent[] { captureVideoIntent });
    
                ((Activity)Forms.Context).StartActivityForResult(chooserIntent, REQUEST_CAMERA);
            }
            return true;
        }
    
    Friday, October 25, 2019 11:15 AM
  • User382871 posted

    If I pick a file upload it it all works well. I just can't take a photo / record video and directly upload it. Add breakpoints for debugging to check the captured image after taking a photo. Compare the results with the method of picking a local file.

    Monday, October 28, 2019 3:35 PM
  • User385718 posted

    Hi Jarvan,

    Due to medical issues I was not able to respond earlier.

    When I use your method I am not able to take a photo or pick a file.

    When I dont use your method and I pick a file it works because the intent data is not empty.

    I need to know how to find the directory to the photo's so I am not only reliable on my intent. Because if I take a photo and want to upload it directly the intent data is always null.

    I have attached 2 files wich are my Mainactivity and my Webchromeclient. Hopefully you or someone else can see what I am doing wrong.

    I am looking forward to you response.

    Wednesday, November 27, 2019 7:55 AM