繁体   English   中英

ParseFile Android C#Xamarin上传/下载图像

[英]ParseFile Android C# Xamarin uploading/downloading images

我已经成功地使用Parse将声音片段上传到设备或从设备下载了声音片段。 我正在尝试类似的操作,但是有图像。 我有一个imageView,其中包含用户选择的图像。 我想将此图像作为嵌套在parseObject(称为“ profileParseObject”)中的parseFile发送

我看到了这篇文章: 将来自图库的图像放在ParseFile android中

但是,使用:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.usman);

导致“名称'Bitmap'在当前上下文中不存在”(我不知道我需要什么程序集引用)。

因此问题是:我无法将imageView中的数据转换为byte []以将其发送以进行解析...。此外,如果成功,我将如何从解析中取回数据,然后将其放入数据返回到imageView?

这是我尝试上传的内容,在此先感谢您的帮助!

byte [] ImageData;
                    ImageData =  _imageView.ToArray<byte>();
                    ParseFile file = new ParseFile("profilePic.png", ImageData);

                    profileParseObject["profilePicture"] = file;

                    if (canUpdate)
                    {
                        await profileParseObject.SaveAsync();
                        Console.WriteLine("Sucessfully updated your information");
                    }
                    else
                    {
                        Console.WriteLine("Cannot update");
                    }

编辑:我更改代码以使用toArray(); 这已经编译,但是我从设备中收到以下错误:

System.InvalidCastException:无法从“ android / widget / ImageView”转换为“ [B””。 在/Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono中的Android.Runtime.JNIEnv.AssertCompatibleArrayTypes(IntPtr sourceArray,System.Type destType)[0x0001a]中。 Android / src / Runtime / JNIEnv.cs:744(位于Android.Runtime.JNIEnv.GetArray [Byte](IntPtr array_ptr)[0x00026] /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd中/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:1207 / Users / builder / data / lanes / monodroid-mlion-中的Java.Lang.Object.ToArray [Byte]()[0x00000] monodroid-4.20系列/ba9bbbdd/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:338在PlugIt.Profile + d__7.MoveNext()[0x005ff]

有任何想法吗!?

ToArray是一种方法,因此您需要添加左括号和右括号。

ImageData = _imageVIew.ToArray<byte>();

编辑:

问题是您正在尝试将imageView控件转换为字节数组。 您需要做的是首先从imageView中获取图像,然后将该图像转换为字节数组。

 //1. Get the Bitmap
BitmapDrawable drawable = imageView.GetDrawable();
Bitmap bitmap = drawable.GetBitmap();

//2. Compress the bitmap into a stream and use that to get the byte array
byte[] imageData;
using(MemoryStream stream = new MemoryStream())
{
    bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
    imageData = stream.ToArray();
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM