Xamarin.android - 将 .jpg 复制到共享文件夹 [英] Xamarin.android - Copy .jpg to shared folder
问题描述
我使用 Samba 文件服务器将文件从我的 Android 设备复制到 Windows 上的共享文件夹.对于 .txt 文件,我没有任何问题,工作正常,但我尝试将 .jpg 文件复制到共享文件夹中,但失败了.我从互联网上搜索了很多代码,但有人解决了我的问题.我设法复制了图像,但是当我打开它时,它已损坏.
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.
谁有任何示例代码?
我的代码是这样的:
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();
感谢所有帮助.
谢谢.
推荐答案
您可以使用 Media.Plugin 从 nuget 先拍照.
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
有 GetStream().
您可以将其用于 Stream,然后将其转换为 byte[].这是一种方法:
You could use this to the Stream and then convert that to a byte[]. Here is one way to do that:
Stream imageStream;
拍照后初始化.
imageStream = file.GetStream();
var imageArr= ReadFully(imageStream );
然后将其写入您的文件夹.
And then write it to your folder .
这篇关于Xamarin.android - 将 .jpg 复制到共享文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!