IOException - 如果无法加载文件 [英] IOException - Cannot load file

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

问题描述

我有一个应用程序,显示GIF图像。 这一切工作正常,如果图像保存在绘制的,我访问它像这样

 是= context.getResources()openRawResource(R.drawable.mygif)。
电影= Movie.de codeStream(是);
 

不过,我需要从互联网上下载的图像,所以我将其保存到CacheDir,工作正常。我想下面的阅读。

 尝试
    {
        是=新的BufferedInputStream(新的FileInputStream(新文件(context.getCacheDir(),mygif.gif)));
    }
    赶上(FileNotFoundException异常E)
    {
        // TODO自动生成的catch块
        e.printStackTrace();
    }
    电影= Movie.de codeStream(是);
 

 电影= Movie.de codeFILE(新文件(context.getCacheDir(),mygif.gif)getPath());
 

但无论怎样,它结束

  java.io.IOException异常
在java.io.InputStream.reset(InputStream.java:218)
在android.graphics.Movie.de codeStream(本机方法)
在android.graphics.Movie.de codeTempStream(Movie.java:74)
在android.graphics.Movie.de codeFILE(Movie.java:59)
 

  java.io.IOException异常:标记已失效。
在java.io.BufferedInputStream.reset(BufferedInputStream.java:350)
在android.graphics.Movie.de codeStream(本机方法)
 

我觉得这是一个非常简单的错误,但我不能克服它。清单有:

 <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

解决方案

建议:

1.try移动下载位置像其他文件夹: SD卡,或应用程序,除了缓存另一个文件夹

2,当初始化的的InputStream ,请尝试使用方法:初始化一个缓冲区大小

  INT缓冲区大小= 16 * 1024;
InputStream的是=新的BufferedInputStream(XXX,缓冲区大小);
 

3.to检查文件存在于你的文件夹

更新:修改code这样的和不使用 Movie.de codeFILE

 的InputStream是= NULL;
    尝试 {
        是=新的BufferedInputStream(新的FileInputStream(新文件(getCacheDir(),mygif.gif)),16 * 1024);
        is.mark(16 * 1024);
    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
电影= Movie.de codeStream(是);
 

没有异常抛出,而电影不为空。

此外,虽然你的code会抛出异常,电影也是不为空。

I have an app that shows GIF images. It all works fine if image is saved in drawable, and I access it like this

is=context.getResources().openRawResource(R.drawable.mygif);
movie = Movie.decodeStream(is);

But I need to download image from internet, so I'm saving it to CacheDir, that works fine. I tried the following to read it.

    try
    {
        is = new BufferedInputStream(new FileInputStream(new File(context.getCacheDir(), "mygif.gif")));
    }
    catch (FileNotFoundException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    movie = Movie.decodeStream(is);

And this

movie = Movie.decodeFile(new File(context.getCacheDir(), "mygif.gif").getPath());

But no matter what, it ends with

java.io.IOException
at java.io.InputStream.reset(InputStream.java:218)
at android.graphics.Movie.decodeStream(Native Method)
at android.graphics.Movie.decodeTempStream(Movie.java:74)
at android.graphics.Movie.decodeFile(Movie.java:59)

or

java.io.IOException: Mark has been invalidated.
at java.io.BufferedInputStream.reset(BufferedInputStream.java:350)
at android.graphics.Movie.decodeStream(Native Method)

I think it's a very simple error, but I can't get over it. Manifest has:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

解决方案

suggestion:

1.try move the download location to another folder like: sdcard, or the app another folder except the cache.

2.when init the inputstream, try use: init with a buffer size

int buffersize = 16*1024;
InputStream  is = new BufferedInputStream(xxx,buffersize);

3.to check the file existed in your folder

update: change the code like this and dont use Movie.decodeFile

 InputStream is = null;
    try {
        is = new BufferedInputStream(new FileInputStream(new File(getCacheDir(), "mygif.gif")), 16 * 1024);
        is.mark(16 * 1024); 
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
movie = Movie.decodeStream(is);

no exception throw, and the movie is not null.

Also, although your code will throw the exception, the movie is also not null.

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

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