1

In my mobile app using Xamarin.Android, I implemented file attachment feature. In that, I got file content in Android.Net.Uri from

protected override async void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
  base.OnActivityResult(requestCode, resultCode, data);
  Android.Net.Uri attachmentUri = data.Data;
}

Now, in order to upload the file as multipart message, I have to convert this uri to byte array. I searched a lot but everything in Java. I need such code in C#.

2 Answers 2

2

You can use the ContentResolver.OpenInputStream on the Android.Net.Uri to retrieve a stream that you can then use StreamContent to convert it to a ByteArrayContent object and apply that to your MultipartFormDataContent.

Example:

using (var resolverStream = ContentResolver.OpenInputStream(data.Data))
using (var streamContent = new StreamContent(resolverStream))
using (var byteArrayContent = new ByteArrayContent(await streamContent.ReadAsByteArrayAsync()))
using (var formDataContent = new MultipartFormDataContent())
{
    formDataContent.Add(byteArrayContent, "file", "DummyFileName");

    // rest of the header setup, PostAsync, etc..

}
Sign up to request clarification or add additional context in comments.

5 Comments

Now I have one more requirements. I need to add more than one files into MultipartFormDataContent. How can I achieve this?
@ThamaraiT Use your MultipartFormDataContent instance and call .Add(...) for each of your "files" (ByteArrayContent).
Like formDataContent.Add(bytearray1,"file","name1"); formDataContent.Add(bytearray2,"file","name2"); , right?
Yes, that is it
Okay, I am checking on it. Once done, I will update you. Thanks for the reply.
0

You can use this for your multipart message:

 var dat = data.Data;
            ByteArrayOutputStream outputstream = new ByteArrayOutputStream();
            FileInputStream filestream;
            try
            {
                filestream = new FileInputStream(new File(dat.Path));
                byte[] buffer = new byte[1024];
                int n = filestream.Read(buffer);
                while (-1 != n)
                {
                    outputstream.Write(buffer, 0, n);
                }
            }
            catch(IOException e)
            {
                e.PrintStackTrace();
             }
            byte[] filebyte = outputstream.ToByteArray();

5 Comments

I think this is Java code. But I need it in C# code. Please check with @SushiHangover's answer. I implemented based on his answer, it works perfectly.
Its c#, i tested it myself
@Tolulope That will work if it is not a content: based uri, otherwise you would need to use the ContentResolver.
@Tolulope Yes. Mine is content uri as told by SushiHangover. Moreover, 'ByteArrayOutputStream' and 'FileInputStream' have reference from 'Java.IO'.
I get the content uri part, but Java .IO name space is in xamarin Android

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.