Xamarin表单的文件选择器不添加从照相机和文件拍摄的选定图像

plupiseo  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(97)

我正在使用Xamarin表单创建一个移动的应用程序,作为我的实现的一部分,我有一个要求,以显示文件选择器上点击相机图标加载到webView和加载图像到webView。目前文件选择器正在显示,但图像没有得到添加。

public override bool OnShowFileChooser(Android.Webkit.WebView webView, Android.Webkit.IValueCallback filePathCallback, FileChooserParams fileChooserParams) {

    Intent takePictureIntent = new Intent(MediaStore.ActionImageCapture);

    if (takePictureIntent.ResolveActivity(mContext.PackageManager) != null) {

        Java.IO.File photoFile = null;

        try {
            string folder = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            photoFile = new Java.IO.File(folder, "image" + DateTime.Now.Millisecond + ".png");
            takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);

            //photoFile = createImageFile();
            //takePictureIntent.PutExtra("PhotoPath", mCameraPhotoPath);
        } catch (Exception e) {
            Console.WriteLine("catch the Exception" + e);
        }
        if (photoFile != null) {
            mCameraPhotoPath = "file:" + photoFile.AbsolutePath;
            //pictureUri = FileProvider.GetUriForFile(mContext, "asdasd", photoFile);
            takePictureIntent.PutExtra(Android.Provider.MediaStore.ExtraOutput, photoFile);
        } else {
            takePictureIntent = null;
        }

    }

    Intent contentSelectionIntent = new Intent(Intent.ActionGetContent);
    contentSelectionIntent.AddCategory(Intent.CategoryOpenable);
    contentSelectionIntent.SetType(file_type);

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

    Intent chooserIntent = new Intent(Intent.ActionChooser);
    chooserIntent.PutExtra(Intent.ExtraIntent, contentSelectionIntent);
    chooserIntent.PutExtra(Intent.ExtraTitle, "File chooser");
    chooserIntent.PutExtra(Intent.ExtraInitialIntents, intentArray);

    //mContext.StartActivity(chooserIntent);
    mContext.StartActivityForResult(chooserIntent, INPUT_FILE_REQUEST_CODE);
    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;

    if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Gingerbread) {
        storageDir = mContext.CacheDir;

    } else {
        storageDir = mContext.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures);
    }
    //new Java.IO.File(mContext.GetExternalFilesDir(null).AbsolutePath);
    Java.IO.File imageFile = Java.IO.File.CreateTempFile(
        imageFileName, /* prefix */
        ".jpg", /* suffix */
        storageDir /* directory */
    );

    return imageFile;

}
daolsyd0

daolsyd01#

首先,请将以下代码添加到AndroidManifest.xml中以声明权限:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.Read_EXTERNAL_STORAGE" />

然后在MainActivity的构造方法中请求权限。

var read = await Permissions.RequestAsync<Permissions.StorageRead>();
var write = await Permissions.RequestAsync<Permissions.StorageWrite>();

最后,请重写Mainactivity的OnActivityResult方法:

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        if (data != null)
        {
            if (requestCode == INPUT_FILE_REQUEST_CODE)// the value of the INPUT_FILE_REQUEST_CODE
            {
                if (null == this.message)
                {
                    return;
                }

                this.message.OnReceiveValue(WebChromeClient.FileChooserParams.ParseResult((int)resultCode, data));
                this.message = null;
            }
        }
    }

相关问题