Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
822 views
in Technique[技术] by (71.8m points)

webview - Xamarin Android Take picture intent save 0b file

i'm trying to take a pitcure with camera startin from fileupload of webview.

All code works for file chooser, but when i take a pitcure with camera, i found a 0b file in the directory where i create the temp file.

Someone can help me?

Here my code for customchromeclient filechooser event:

        public override bool OnShowFileChooser(Android.Webkit.WebView webView, Android.Webkit.IValueCallback filePathCallback, FileChooserParams fileChooserParams)
    {
        string[] type = fileChooserParams.GetAcceptTypes();
        this.mFilePathCallback = filePathCallback;
        px_MyActivity.mFilePathCallback = filePathCallback;

        if (string.Join("/", type).Contains("jpg") || string.Join("/", type).Contains("image"))
        {
            try
            {

                    Intent takePictureIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);    
                    if (takePictureIntent.ResolveActivity(px_MyActivity.PackageManager) != null) {
                        Java.IO.File photoFile = null;
                        try
                        {
                            photoFile = createImageFile();
                        }
                        catch(Exception e)
                        {

                        }
                        if (photoFile != null) {

                            mCameraPhotoPath = "file:" + photoFile.AbsolutePath;
                            //takePictureIntent.PutExtra(Android.Provider.MediaStore.ExtraOutput, photoFile);

                            px_MyActivity.mCameraPhotoPath = Android.Net.Uri.FromFile(photoFile); ;
                        }  else {
                            takePictureIntent = null;
                        }
                    }

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

                    Intent[] intentArray;
                    if(takePictureIntent != null) {
                        intentArray = new Intent[]{takePictureIntent};
                    } else {
                        intentArray = new Intent[0];
                    }

                    //Intent chooserIntent = new Intent(Intent.ActionChooser);
                    Intent chooserIntent = Intent.CreateChooser(contentSelectionIntent, "Carica Foto");
                    chooserIntent.PutExtra(Intent.ExtraIntent, contentSelectionIntent);
                    chooserIntent.PutExtra(Intent.ExtraTitle, "Carica Foto");
                    chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);

                    px_MyActivity.StartActivityForResult(chooserIntent, PHOTO_REQUEST);

            }catch(Exception ex){
            }
        }
        else if (string.Join("/", type).Contains("mpg"))
        {
            Intent takePictureIntent = new Intent(Android.Provider.MediaStore.ActionVideoCapture);
        }
        else
        {
            Intent chooserIntent = fileChooserParams.CreateIntent();
            chooserIntent.SetType("file/*");
            chooserIntent.AddCategory(Intent.CategoryOpenable);
            px_MyActivity.StartActivity(Intent.CreateChooser(chooserIntent, "File Chooser"), FILECHOOSER_RESULTCODE, this.OnFileChooserEnd);
        }
        return true;
    }
    private Java.IO.File createImageFile(){
        // Create an image file name
        string timeStamp = Android.OS.SystemClock.CurrentThreadTimeMillis().ToString();
        string imageFileName = "JPEG_" + timeStamp + "_";
        Java.IO.File storageDir = new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.Path);
        Java.IO.File imageFile = Java.IO.File.CreateTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        return imageFile;
    }

and here the code for avtivityresult:

        protected override void OnActivityResult(int requestCode, Result resultVal, Android.Content.Intent data)
    {
        if (resultVal == Result.Ok)
        {
            if (requestCode == SPEECH)
            {
                var matches = data.GetStringArrayListExtra(RecognizerIntent.ExtraResults);
                if (matches.Count != 0)
                {
                    string xResult = matches[0];
                    if (xResult.Length > 500)
                    {
                        xResult = xResult.Substring(0, 500);
                    }
                    Xamarin.Forms.MessagingCenter.Send(this, "SpeechEnd", xResult);
                }
            }else if (requestCode == FILECHOOSER_RESULTCODE)
            {
            }else if (requestCode == PHOTO_REQUEST)
            {
                if (mFilePathCallback == null)
                {
                    base.OnActivityResult(requestCode, resultVal, data);
                    return;
                }
                Android.Net.Uri[] result = null;
                if (data == null)
                {
                    if (mCameraPhotoPath != null)
                    {
                        result = new Android.Net.Uri[] { mCameraPhotoPath };
                    }
                }
                else
                {
                    string xDataString = data.DataString;
                    if (xDataString != null)
                    {
                        result = new Android.Net.Uri[] { Android.Net.Uri.Parse(xDataString) };
                    }
                    else
                    {
                        Android.Graphics.Bitmap xImage = (Android.Graphics.Bitmap)data.Extras.Get("data");
                        try
                        {
                            //if(Android.Support.V4.Content.ContextCompat.CheckSelfPermission(this, Android.Manifest.Permission.ReadExternalStorage) == Permission.Granted)
                            //{
                            //    var xStream = new System.IO.FileStream(mCameraPhotoPath.Path, System.IO.FileMode.OpenOrCreate);

                            //    xImage.Compress(Bitmap.CompressFormat.Jpeg, 100, xStream);
                            //    xStream.Flush();
                            //    xStream.Close();

                            //    //string path = Android.Provider.MediaStore.Images.Media.InsertImage(ContentResolver, xImage, "Title", "prova 1");
                            //   // string path = Android.Provider.MediaStore.Images.Media.InsertImage(ContentResolver, mCameraPhotoPath.Path, "Title.jpg", "prova 1");
                            //}


                            if (mCameraPhotoPath != null)
                            {
                                result = new Android.Net.Uri[] { mCameraPhotoPath };
                            }
                        }
                        catch (Java.IO.IOException e)
                        {
                            e.PrintStackTrace();
                        }
                    }
                }
                //mFilePathCallback.OnReceiveValue(Android.Webkit.WebChromeClient.FileChooserParams.ParseResult((int)resultVal, data));
                mFilePathCallback.OnReceiveValue(result);
                mFilePathCallback = null;
            }
        }else{
            if (requestCode == PHOTO_REQUEST)
            {
                mFilePathCallback.OnReceiveValue(Android.Webkit.WebChromeClient.FileChooserParams.ParseResult((int)resultVal, null));
            }
        }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...