简体   繁体   中英

ParseFile Android C# Xamarin uploading/downloading images

I've successfully used Parse to upload and download sound clips to/from the device. I'm trying a similar thing, but with images. I have an imageView, which contains an image selected by the user. I want to send this image as a parseFile nested inside a parseObject (called "profileParseObject")

I saw this post: Putting image from gallery in ParseFile android

However,using:

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

causes 'the name 'Bitmap' does not exist in the current context' (I don't know what assembly reference I need).

So the issue is: I Can't convert the data in the imageView into a byte[] to send it up to parse.... Also, if I was successful, how would I go about retrieving it back from parse then putting that data back into the imageView?

here's my attempt to upload, and thanks in advance for any help!

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");
                    }

EDIT: I changed the code to use toArray(); this has compiled but I get the following error from the device:

System.InvalidCastException: Unable to cast from 'android/widget/ImageView' to '[B'. at Android.Runtime.JNIEnv.AssertCompatibleArrayTypes (IntPtr sourceArray, System.Type destType) [0x0001a] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:744 at Android.Runtime.JNIEnv.GetArray[Byte] (IntPtr array_ptr) [0x00026] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Runtime/JNIEnv.cs:1207 at Java.Lang.Object.ToArray[Byte] () [0x00000] in /Users/builder/data/lanes/monodroid-mlion-monodroid-4.20-series/ba9bbbdd/source/monodroid/src/Mono.Android/src/Java.Lang/Object.cs:338 at PlugIt.Profile+d__7.MoveNext () [0x005ff]

Any ideas!?

ToArray is a method and therefore you need to add the open and closed brackets.

ImageData = _imageVIew.ToArray<byte>();

Edit:

The problem is that your'e trying to convert the imageView control into the byte array. What you need to do is to first fetch the image from the imageView and then convert that image into the byte array.

 //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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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