Xamarin.android - Copy .jpg to shared folder

▼魔方 西西 提交于 2019-12-20 03:52:20

问题


I'm using a Samba File Server for copy files from my Android device to a shared folder on Windows. With .txt files i haven't any problems, works fine, but I tried to copy a .jpg file into shared folder and it fails. I searched a lot of codes from internet but anyone solved my problem. I managed to copy the image, but when I open it, is damaged.

Does anyone have any sample code?

My code is this:

Java.IO.File mfile = new Java.IO.File(item.FullName);

var mSharedFolderCalidad = new SmbFile(AppGlobalConfigSoftware.Instance.Parameters.PathToDownloadCalidad + item.Name);

//If exists don't create another time
if (!mSharedFolderCalidad.Exists())
     mSharedFolderCalidad.CreateNewFile();

InputStream inFile = new FileInputStream(mfile);

SmbFileOutputStream sfos = new SmbFileOutputStream(mSharedFolderCalidad);

byte[] buf = new byte[1024];
int len;
while ((len = inFile.Read(buf)) > 0) 
{
  sfos.Write(buf, 0, len);
}

inFile.Close();
sfos.Close();

All help is appreciated.

Thank you.


回答1:


You can use Media.Plugin from nuget to take photo firstly.

var file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
    PhotoSize = PhotoSize.Medium,
});
public  byte[] ReadFully(Stream input)
{
    byte[] buffer = new byte[16*1024];
    using (MemoryStream ms = new MemoryStream())
    {
        int read;
        while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            ms.Write(buffer, 0, read);
        }
        return ms.ToArray();
    }
}

MediaFile has GetStream().

You could use this to the Stream and then convert that to a byte[]. Here is one way to do that:

Define a stream

Stream imageStream;

And init it after you take the photo .

imageStream = file.GetStream();

var imageArr= ReadFully(imageStream );

And then write it to your folder .



来源:https://stackoverflow.com/questions/57709184/xamarin-android-copy-jpg-to-shared-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!