使用SharpCifs和Decode从NAS加载图像不起作用
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用SharpCifs和Decode从NAS加载图像不起作用相关的知识,希望对你有一定的参考价值。
我想使用带有VPN-Connection的APP通过使用SharpCifs从NAS服务器加载图像。
我得到一个Stream但是在Bitmap.Factory.DecodeStream上,结果位图为空
为什么我能从MemoryStream中获取图片正确?
使用SharpCifs.Smb;
namespace AppSons
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)]
public class MainActivity : AppCompatActivity, NavigationView.IOnNavigationItemSelectedListener
{
ImageView networkImage;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.activity_main);
// more Code
networkImage = FindViewById<ImageView>(Resource.Id.imageViewNetwork);
Bitmap bmpa = BitmapFactory.DecodeStream(GetImageBitmapFromPath("smb://192.168.1.10//Media/b.bmp"));
networkImage.SetImageBitmap(bmpa);
}
private MemoryStream GetImageBitmapFromPath(string path)
{
var auth1 = new NtlmPasswordAuthentication("admin:admin");
var imageFile = new SmbFile(path, auth1);
var memStream = new MemoryStream();
if (imageFile.Exists())
{
var readStream = imageFile.GetInputStream();
var buffer = new byte[1024 * 8];
int size;
while ((size = readStream.Read(buffer, 0, buffer.Length)) > 0)
{
memStream.Write(buffer, 0, size);
}
readStream.Dispose();
return memStream;
}
else
{
return memStream;
}
}
}
}
答案
我找到了解决方案:
using SharpCifs.Smb;
protected override void OnCreate(Bundle savedInstanceState)
{
networkImage = FindViewById<ImageView>(Resource.Id.imageViewNetwork);
GetImageBitmapFromPath("smb://192.168.1.100/Media/Media/test.bmp");
}
private async void GetImageBitmapFromPath(string path)
{
byte[] imst = await LoadPic(path);
Bitmap bmpa = BitmapFactory.DecodeByteArray(imst, 0, imst.Length);
networkImage.SetImageBitmap(bmpa);
}
private async Task<byte[]> LoadPic(string path)
{
var auth1 = new NtlmPasswordAuthentication("admin:admin");
var imageFile = new SmbFile(path, auth1);
var memStream = new MemoryStream();
if (imageFile.Exists())
{
var readStream = imageFile.GetInputStream();
((Stream)readStream).CopyTo(memStream);
return memStream.ToArray();
}
else
{
return null;
}
}
以上是关于使用SharpCifs和Decode从NAS加载图像不起作用的主要内容,如果未能解决你的问题,请参考以下文章