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