Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOException - Cannot load file

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"/>
like image 859
Bojan Kogoj Avatar asked Apr 20 '12 03:04

Bojan Kogoj


2 Answers

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.

like image 145
idiottiger Avatar answered Sep 20 '22 12:09

idiottiger


I know is a really old post, but I was having the same issue

Copying the InputStream to a byte array and calling Movie.decodeByteArray worked for me, even on Samsung Devices

like image 39
BeNeXuS Avatar answered Sep 18 '22 12:09

BeNeXuS