从SDCard加载文件 [英] Load File from SDCard

查看:63
本文介绍了从SDCard加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将

问题:如何将文件作为源提供给epubView控件

这是正确的方法吗,请对此提出建议.谢谢

解决方案

尽管我没有尝试过您的方法,但是我无法确切地说出错误所在(也许SD的文件被异步读取,因此您得到EndOfStream,并且请记住,正如EPUB Reader网站上所说的那样-该网站正在大力开发中).检查将文件复制到ISolatedStorage后是否可以使用它.
在这种情况下,我会先尝试从SD复制到内存流,如下所示:

  ExternalStorageDevice sdCard =(等待ExternalStorage.GetExternalStorageDevicesAsync()).FirstOrDefault();如果(sdCard!= null){MemoryStream newStream =新的MemoryStream();使用(ExternalStorageFile文件=等待sdCard.GetFileAsync(_sdFilePath))使用(流SDfile =等待文件.OpenForReadAsync())newStream =等待ReadToMemory(SDfile);ePubView.Source = newStream;} 

和ReadToMemory:

 私有异步任务< MemoryStream>ReadToMemory(Stream streamToRead){MemoryStream targetStream =新的MemoryStream();const int BUFFER_SIZE = 1024;byte [] buf =新的byte [BUFFER_SIZE];int bytesread = 0;而((bytesread =等待streamToRead.ReadAsync(buf,0,BUFFER_SIZE))> 0){targetStream.Write(buf,0,bytesread);}返回targetStream;} 

也许会有所帮助.

I integrated (this)EPUB Reader reader to my project. It is working fine. & I want to load the file from SDCard instead of Isolated storage of device

To open file from Isolated storage we have IsolatedStorageFileStream like this

IsolatedStorageFileStream isfs;
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
    try
    {
        isfs = isf.OpenFile([Path to file], FileMode.Open);
    }
    catch
    {
        return;
    }
}

ePubView.Source = isfs;

For file in SDcard I tried like this

ExternalStorageDevice sdCard = (await ExternalStorage.GetExternalStorageDevicesAsync()).FirstOrDefault();

// If the SD card is present, get the route from the SD card.
if (sdCard != null)
{
    ExternalStorageFile file = await sdCard.GetFileAsync(_sdFilePath);
    // _sdFilePath is string that having file path of file in SDCard

    // Create a stream for the route.
    Stream file = await file.OpenForReadAsync();

    // Read the route data.
    ePubView.Source = file;
 }

Here I am getting exception System.IO.EndOfStreamException

If You want try.. Here is my project sample link

Question : How can I give my file as source to epubView control

Is this is proper way, please give a suggestion regarding this.. Thanks

解决方案

Although I've not tried your approach, and I cannot say exactly where is an error (maybe file from SD is read async and thus you get EndOfStream, and please keep in mind that as it is said at EPUB Reader Site - it's under heavy developement). Check if after copying the file to ISolatedStorage, you will be able to use it.
I would try in this case first copying from SD to Memory stream like this:

ExternalStorageDevice sdCard = (await ExternalStorage.GetExternalStorageDevicesAsync()).FirstOrDefault();

if (sdCard != null)
{
   MemoryStream newStream = new MemoryStream();
   using (ExternalStorageFile file = await sdCard.GetFileAsync(_sdFilePath))
    using (Stream SDfile = await file.OpenForReadAsync())
      newStream = await ReadToMemory(SDfile);

   ePubView.Source = newStream;
} 

And ReadToMemory:

private async Task<MemoryStream> ReadToMemory(Stream streamToRead)
{
   MemoryStream targetStream = new MemoryStream();

   const int BUFFER_SIZE = 1024;
   byte[] buf = new byte[BUFFER_SIZE];
   int bytesread = 0;
   while ((bytesread = await streamToRead.ReadAsync(buf, 0, BUFFER_SIZE)) > 0)
   {
       targetStream.Write(buf, 0, bytesread);
   }
   return targetStream;
}

Maybe it will help.

这篇关于从SDCard加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆