【问题标题】:Xamarin Android Gallary Photo Stream has null baseXamarin Android 画廊照片流具有空基
【发布时间】:2018-10-29 10:45:08
【问题描述】:

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

创建意图

    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);
    }

MainActivity.cs 中的 OnActivityResult

  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);

然后我有一个从流中获取字节的简单方法

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] 在 :0 处 System.IO.Stream.InternalCopyTo(System.IO.Stream 目标, System.Int32 bufferSize) [0x00015] in :0 在 System.IO.Stream.CopyTo (System.IO.Stream 目标)[0x00099] 在 :0 在(包装 remoting-invoke-with-check) System.IO.Stream:CopyTo (System.IO.Stream) 在 DAFCheckMobile_DEV.Controls.CTLUploadImage.GetBytes () [0x0001d] 中 E:\Data\Solutions\DAF\DAFcheckMobile\DAFcheckMobile_DEV\DAFCheckMobile_DEV\DAFCheckMobile_DEV\Controls\CTLUploadImage.cs:325 }

我注意到流的唯一不同之处在于 stream.BaseInputStream 为空。

我已经从高到低搜索了这个问题的答案,尽管有类似的问题我找不到有相同问题的人或有效的解决方案。

我正在三星 SM-G920F(Android 7.0 - API 24)上调试它

任何帮助都将不胜感激,我已经在这个问题上撞墙了一段时间,如果帖子过于冗长,请见谅。

【问题讨论】:

    标签: c# android-intent xamarin.android inputstream memorystream


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-03
      • 1970-01-01
      • 2012-09-12
      • 1970-01-01
      相关资源
      最近更新 更多