xamarin.android - 从 URI 获取位图

标签 xamarin.android

我正在从图库中选择图片。
这是我正在使用的代码

            Intent intent = new Intent();
            intent.SetType("image/*");
            intent.SetAction(Intent.ActionGetContent);
            this.StartActivityForResult(Intent.CreateChooser(intent,
                    "Select Picture"), SelectPicture);

我在 data.DataString content://media/external/images/media/11 中获取 Activty 结果中的这个字符串..
这不是所选图像的完整路径。但最终我想将其转换为位图。

在事件结果中..

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)

Bitmap bitmap = (Android.Graphics.Bitmap)data.Extras.Get("data");

给出空错误。

当我从相机捕获图像时,如何工作正常而不是从图库中选择图像,我得到了位图。我正在使用的代码:

 var cameraIntent = new Intent(MediaStore.ActionImageCapture);
 cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
 this.StartActivityForResult(cameraIntent, TakePicture);

最佳答案

要从 Uri 转换为位图,请遵循以下示例:convertUriToBitmap . 在monodroid中代码如下:

private Android.Graphics.Bitmap NGetBitmap(Android.Net.Uri uriImage)
    {
        Android.Graphics.Bitmap mBitmap = null;
         mBitmap = Android.Provider.MediaStore.Images.Media.GetBitmap(this.ContentResolver, uriImage);
         return mBitmap;
    }

关于xamarin.android - 从 URI 获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10649122/

相关文章:

java - 适用于 Xamarin Android 的类似 Chrome 的标签切换器

android - Xamarin/Mono for Android - 自定义标题 View 时 AlertDialog 崩溃

c# - RecyclerView 中的 OxyPlot -MVVMCross Xamarin.Android

c# - Mvx绑定(bind) :Error: View type not found - mvvmemiextensions. EmiDatePicker

c# - 如何运行 xamarin 表单设计器?

c# - 如何在 Xamarin 的本地通知中显示应用名称?

android - 如何在我的应用程序不崩溃的情况下传递 DialogInterface OnDismissListener?

android - 使用 NuGet 安装 GooglePlayServices 时出现安装包错误

c# - 如何在Android 项目中使用MySql.Data.dll?

android - Android 单声道 : Which Testing Frameworks work