欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > 内容正文

WP_从独立存储区读取缓存的图片,wp缓存

来源: 开发者 投稿于  被查看 31431 次 评论:156

WP_从独立存储区读取缓存的图片,wp缓存


 

 
    ///<summary>
    /// 独立存储缓存的图片源
    /// 用法:item.img = new StorageCachedImage(newUri(http://www.baidu.com/12.jpg));
    ///</summary>
    public sealed class StorageCachedImage : BitmapSource
    {
        private readonly Uri uriSource;
        private readonly string filePath;
        private const string CacheDirectory = "CachedImages";
        static StorageCachedImage()
        { 
            //创建缓存目录
            using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (!isolatedStorageFile.DirectoryExists(CacheDirectory))
                {
                    isolatedStorageFile.CreateDirectory(CacheDirectory);
                }
            }
        }
        ///<summary>
        /// 创建一个独立存储缓存的图片源
        ///</summary>
        ///<param name="uriSource"></param>
        public StorageCachedImage(Uri uriSource)
        {
            this.uriSource = uriSource;
            string sUrl = uriSource.AbsolutePath;
            //文件路径
            filePath = Path.Combine(CacheDirectory, sUrl.Substring(sUrl.LastIndexOf("/") + 1, sUrl.Length - sUrl.LastIndexOf("/") - 1));
            OpenCatchSource();
        }
        ///<summary>
        /// 打开缓存源
        ///</summary>
        private void OpenCatchSource()
        {
            //网络可用时,下载图片(网络不可用时读取本地缓存)
            if (CommonConst.CheckNetWorking())
            {
                SetWebStreamSource();
            }
            else
            {
                bool exist;
                using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
                {
                    exist = isolatedStorageFile.FileExists(filePath);
                }
                if (exist)
                {
                    SetCacheStreamSource();
                }
                else
                {
                    //SetWebStreamSource();
                }
            }
        }
        ///<summary>
        /// 设置缓存流到图片
        ///</summary>
        private void SetCacheStreamSource()
        {
            using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
            using (var stream = isolatedStorageFile.OpenFile(filePath, FileMode.Open, FileAccess.Read))
            {
                SetSource(stream);
            }
        }
        ///<summary>
        /// 下载Uri中的图片
        ///</summary>
        private void SetWebStreamSource()
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create(uriSource);
            httpWebRequest.AllowReadStreamBuffering = true;
            httpWebRequest.BeginGetResponse(ResponseCallBack, httpWebRequest);
        }
        ///<summary>
        /// 下载回调
        ///</summary>
        ///<param name="asyncResult"></param>
        private void ResponseCallBack(IAsyncResult asyncResult)
        {
            var httpWebRequest = asyncResult.AsyncState as HttpWebRequest;
            if (httpWebRequest == null) return;
            try
            {
                var response = httpWebRequest.EndGetResponse(asyncResult);
                using (var stream = response.GetResponseStream())
                using (var isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication())
                using (var fileStream = isolatedStorageFile.OpenFile
                (filePath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    CopyStream(stream, fileStream);
                }
                Dispatcher.BeginInvoke(SetCacheStreamSource);
            }
            catch (Exception err)
            {
                //Debug.WriteLine(err.Message);
            }
        }
        private static void CopyStream(System.IO.Stream input, IsolatedStorageFileStream output)
        {
            byte[] buffer = new byte[32768];
            long TempPos = input.Position;
            int readCount;
            do
            {
                readCount = input.Read(buffer, 0, buffer.Length);
                if (readCount > 0)
                {
                    output.Write(buffer, 0, readCount);
                }
            } while (readCount > 0);
            input.Position = TempPos;
        }
    }

小米手机清除了媒体存储器的缓存后,不可以读取照片与歌曲,怎恢复?

如果重启手机之后,还是无法显示:
解决办法:
1.连接助手类软件看看能否查看到照片歌曲,导出照片及歌曲,然后格式化USB存储器,重新导入。
2.解决不了,恢复出厂设置,但不格式化USB存储器,向导完之后,看看能否查看照片。
3.上面两个步骤解决不了,只能进行照片、歌曲恢复。如需帮助,私信联
 

图片链接可缓存最多24小时,不得直接存储或缓存图片本身了,帮忙

图片链接后的ID地址,不用考虑,键接打开方式,点击新窗口打开.图片尺寸,高及宽不用考虑(主要是模块大小,在空间主页上,我们可以自己调整),模块背景边框,点击无 最后输入验证码 点击确定,在空间的左上角,就出现了你刚刚制作的图片模块(有可能显示为全灰色,不过不用担心,当前出现的图片为全灰色,不要误认为叉入不成功) .你可以拖动模块大小,调整你所准备放置的位置. htt最后一定要在自定义的下方,点击保存,这样 这张图片,就会显示在你的空间主页上啦. 效果演示如下: 我选择的图片ID地址 图片名称--韩国精美黑白个性动态图片(240×320) a href=".qqbye/gif/UploadFiles_gif/200805/2008052302393182.gif" rel=nofollow.qqbye/gif/UploadFiles_gif/200805/2008052302393182.gif/a
 

相关频道:

用户评论