简体   繁体   English

从 mono 上的本地存储读取图像文件,用于 android

[英]Reading an image file from local storage on mono for android

In mono for android I have an app that saves images to local storage for caching purposes.在 mono for android 中,我有一个应用程序可以将图像保存到本地存储以进行缓存。 When the app launches it tries to load images from the cache before trying to load them from the web.当应用程序启动时,它会先尝试从缓存中加载图像,然后再尝试从 web 加载图像。

I'm currently having a hard time finding a good way to read and load them from local storage.我目前很难找到一种从本地存储读取和加载它们的好方法。

I'm currently using something equivilant to this:我目前正在使用与此等效的东西:

        List<byte> byteList = new List<byte>();
        using (System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(context.OpenFileInput("filename.jpg")))
        {
            while (binaryReader.BaseStream.IsDataAvailable())
            {
                byteList.Add(binaryReader.ReadByte());
            }
        }
        return byteList.toArray();

OpenFileInput() returns a stream that does not give me a length so I have to read one byte at a time. OpenFileInput() 返回一个 stream,它没有给我一个长度,所以我必须一次读取一个字节。 It also can't seek.它也无法寻求。 This seems to be causing images to load much slower than they aughto.这似乎导致图像加载速度比实际加载速度慢得多。 Loading images from Resrouce.Drawable is almost instantanious by comparison, but with my method there a very noticable pause, maybe 300ms, for loading a 8kb file.相比之下,从 Resrouce.Drawable 加载图像几乎是瞬时的,但使用我的方法加载 8kb 文件时会有一个非常明显的停顿,可能是 300 毫秒。 This seems like a really obvious task to be able to do, but I've tried many solutions and searched a lot for advise but to no avail.这似乎是一项非常明显的任务,但我已经尝试了很多解决方案并搜索了很多建议但无济于事。

I've also noticed this code seems to crash with an EndOfStream exception when not run on the UI thread.我还注意到这段代码似乎在不在 UI 线程上运行时因 EndOfStream 异常而崩溃。

Any help would be hugely appreciated任何帮助将不胜感激

What do you intend on doing with the List<byte> ?你打算用List<byte>做什么? You want to "load images from the cache," but you don't specify what you want to load them into .您想要“从缓存中加载图像”,但没有指定要将它们加载.

If you want to load them into aAndroid.Graphics.Bitmap , you could use BitmapFactory.DecodeStream(Stream) :如果你想将它们加载到Android.Graphics.Bitmap中,你可以使用BitmapFactory.DecodeStream(Stream)

Bitmap bitmap = BitmapFactory.DecodeStream(context.OpenFileInput("filename.jpg"));

This would remove the List<byte> intermediary.这将删除List<byte>中介。

If you really need all the bytes (for whatever reason), you can rely on the fact that System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal) is the same as Context.FilesDir , which is what context.OpenFileInput() will use, permitting:如果您确实需要所有字节(无论出于何种原因),您可以依赖System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal)Context.FilesDir相同的事实,这就是context.OpenFileInput()将使用,允许:

byte[] bytes = System.IO.File.ReadAllBytes(
        Path.Combine (
            System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
            "filename.jpg"));

However, if this is truly a cache, you should be using Context.CacheDir instead of Context.FilesDir , which is Path.GetTempPath returns:但是,如果这确实是一个缓存,您应该使用Context.CacheDir而不是Context.FilesDir ,这是Path.GetTempPath返回的:

byte[] cachedBytes = System.IO.File.ReadAllBytes(
        Path.Combine(System.IO.Path.GetTempPath(), "filename.jpg"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM