我已经成功地使用Parse将声音片段上传到设备或从设备下载。我也在尝试类似的事情,但有图像。我有一个imageView,它包含用户选择的图像。我想将此图像作为嵌套在parseObject(称为“profileParseObject”)中的parseFile发送
我看到了这篇文章:将图库中的图像放入 ParseFile android
但是,使用:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.usman);
导致“当前上下文中不存在名称”位图“”(我不知道需要什么程序集引用)。
所以问题是:我无法将图像视图中的数据转换为字节[]以将其发送到解析....另外,如果我成功了,我将如何从解析中检索它,然后将该数据放回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();这已编译,但我从设备中得到以下错误:
系统。InvalidCastException:无法从“android/widget/ImageView”转换为“[B]”。在Android。runtime . jnienv . assertcompatiblearraytypes(IntPtr source array,System。在/Users/builder/data/lanes/Mono droid-mlion-Mono droid-4.20-series/ba 9 bbbdd/source/Mono droid/src/Mono中键入destType) [0x0001a]。Android上的Android/src/Runtime/jnienv . cs:744。/Users/builder/data/lanes/Mono droid-mlion-Mono droid-4.20-series/ba 9 bbbdd/source/Mono droid/src/Mono中的runtime . JNI env . get array[Byte](int ptr array _ ptr)[0x 00026]。Android/src/Runtime/jnienv . cs:1207 at Java。/Users/builder/data/lanes/Mono droid-mlion-Mono droid-4.20-series/ba 9 bbbdd/source/Mono droid/src/Mono中的lang . object . to array[Byte]()[0x 00000]。Android/src/Java。Lang/Object.cs:338 at PlugIt。简档d__7。MoveNext () [0x005ff]
有想法吗!?
ToArray是一个方法,因此您需要添加开括号和闭括号。
图像数据 = _imageVIew.ToArray
编辑:
问题是您尝试将 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();
}