首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Xamarin Android画廊照片流的碱基为空

Xamarin Android画廊照片流的碱基为空
EN

Stack Overflow用户
提问于 2018-10-29 10:45:08
回答 1查看 123关注 0票数 0

我试图从图片库中得到一个图像,然后从那个图像中获取字节。这个概念适用于PDF和视频,尽管对于图像,我得到的BaseInputStream流是空的,因此当试图复制到内存流(我相信)时会出现错误,我尝试了不同的意图,并在复制之前将位置设置为0,但没有占上风。所有相关代码如下。

创造意图

代码语言:javascript
复制
    public const int GalleryPhoto = 4;
    public const int GalleryVideo = 5;
    public const int SelectFile = 7;

void IDevice.GetImageFromGallery()
    {
        if (_dir == null)
        { CreateDirectoryForPictures(); }
        Intent imageIntent = new Intent();
        imageIntent.SetType("image/*");
        imageIntent.SetAction(Intent.ActionGetContent);
        MyApp.Droid.MainActivity.Main.StartActivityForResult(imageIntent, GalleryPhoto);
    }

    void IDevice.SelectFile()
    {
        if (_dir == null)
        { CreateDirectoryForPictures(); }
        Intent imageIntent = new Intent();
        imageIntent.SetType("application/pdf");
        imageIntent.SetAction(Intent.ActionOpenDocument);
        MyApp.Droid.MainActivity.Main.StartActivityForResult(imageIntent, SelectFile);
    }

    void IDevice.GetVideoFromGallery()
    {
        if (_dir == null)
        { CreateDirectoryForPictures(); }
        Intent imageIntent = new Intent();
        imageIntent.SetType("video/*");
        imageIntent.SetAction(Intent.ActionGetContent);
        MyApp.Droid.MainActivity.Main.StartActivityForResult(imageIntent, GalleryVideo);
    }

OnActivityResult in MainActivity.cs

代码语言:javascript
复制
  protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        {
            if (resultCode == Result.Ok)
            {

                if (requestCode == AndroidDevice.GalleryPhoto)
                {
                    selectedFile = data.Data;
                    System.IO.Stream stream = ContentResolver.OpenInputStream(data.Data);
                    AndroidDevice._stream = stream;
                    AndroidDevice.ImageHasBeenTaken();
                }
                else if (requestCode == AndroidDevice.GalleryVideo)
                {
                    selectedFile = data.Data;
                    System.IO.Stream stream = ContentResolver.OpenInputStream(data.Data);
                    AndroidDevice._stream = stream;
                    AndroidDevice.VideoHasBeenTaken();
                }
                else if (requestCode == AndroidDevice.SelectFile)
                {
                    selectedFile = data.Data;
                    System.IO.Stream stream = ContentResolver.OpenInputStream(data.Data);
                    AndroidDevice._stream = stream;
                    AndroidDevice.FileHasBeenSelected();

                }
            }
            GC.Collect();
        }
    }

我将流保存在一个对象中,并使用它来显示图像(如果它是我选择的图像),然后使用它来获取要上传的字节。

显示图像在流中运行良好。

this.ImageObj.Source = ImageSource.FromStream(()=>stream);

然后,我有了一种从流中获取字节的简单方法。

代码语言:javascript
复制
public byte[] GetBytes()
    {
        byte[] bytes = new byte[0];
        try
        {                
            if (stream!= null)
            {
                MemoryStream ms  = new MemoryStream();
                stream.CopyTo(ms);
                bytes = ms.ToArray();                  
            }
            return bytes;
        }
        catch(Exception ex)
        {
            Screens.DisplayExcpetion de = new Screens.DisplayExcpetion(ex);
            return bytes;
        }
    }

这适用于PDF和视频,但是当我选择一个图像时,我会得到以下错误

{System.NullReferenceException:未设置为对象实例的对象引用。在Android.Runtime.InputStreamInvoker.Read (System.Byte[]缓冲器,System.Int32偏移量,System.Int32计数) 0x00006 in :0 at System.IO.Stream.InternalCopyTo (System.IO.Stream System.IO.Stream.InternalCopyTo,0x00015 in DAFCheckMobile_DEV.Controls.CTLUploadImage.GetBytes at System.IO.Stream.CopyTo (System.IO.Stream目的) 0x00099 in <05f5f767747c4c30800c25866dc4cdd4>:0 at (包装器远程调用与检查) System.IO.Stream:CopyTo (System.IO.Stream) at <05f5f767747c4c30800c25866dc4cdd4>:0 () 0x0001d in System.IO.Stream.CopyTo}

关于流,我注意到的唯一不同之处是,stream.BaseInputStream是空的。

我已经搜索了一个很低的答案,虽然也有类似的问题,我找不到有相同问题或解决方案的人。

我正在三星SM-G920F (Android7.0- API 24)上调试这个程序。

任何帮助都将是非常感谢的,我已经把我的头撞在墙上一段时间了,如果柱子太长,我很抱歉。

EN

回答 1

Stack Overflow用户

发布于 2018-10-29 12:41:50

使用流获取图像源似乎是导致BaseInputStream变为空的原因,首先获取字节,然后从这些字节获取图像源解决了问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53043812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档