Android - Error loading gif file using Movie class

I am trying to use android.graphics.Movieto play a GIF file from /sdcard/download.

If I put the file in a folder drawablein the APK, I can upload it using:

InputStream istr = context.getResources().openRawResource(R.drawable.animfile);
Movie movie = Movie.decodeStream(istr);

It works. movie.duration()will show the correct duration that I use to get the value for movie.setTime().

The problem occurs if instead of drawable I try to load it from an SD card using

String path = Environment.getExternalStorageDirectory() + "/download/animfile.gif";
Movie movie = Movie.decodeFile(path);

Something seems to be loading because movieit is not null. But the problem is what movie.duration()returns 0.

Any idea why this is happening, and what should I do?

+5
source share
3

. getContentResolver(). OpenInputStream streamToBytes android BitmapDecode, , , , , - sd .

:

...
Uri uri = Uri.parse(uriString);
java.io.InputStream is;
try {
    is = context.getContentResolver().openInputStream(uri);
}
catch(Exception e) {
}
byte[] array = streamToBytes(is);
Movie movie = Movie.decodeByteArray(array, 0, array.length);


private static byte[] streamToBytes(InputStream is) {
    ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
    byte[] buffer = new byte[1024];
    int len;
    try {
        while ((len = is.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
    } catch (java.io.IOException e) {
    }
    return os.toByteArray();
}
+3

, . Uri ContentResolver. ,

Movie.decodeByteArray(array, 0, array.length)

Movie.decodeFile(path);

reset() , decodeFile ().

+2
 is = new FileInputStream(file path);



                if (path != null) {
                            try {
                                byte[] array = streamToBytes(is);
                                mMovie =  Movie.decodeByteArray(array, 0, array.length);

                                mDuration = mMovie.duration();
                                Log.v("total time ", mDuration + "");
                            } finally {
                                is.close();
                            }
                        } else {
                            throw new IOException("Unable to open R.raw.");
                        }


 private byte[] streamToBytes(InputStream is) {
                ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
                byte[] buffer = new byte[1024];
                int len;
                try {
                    while ((len = is.read(buffer)) >= 0) {
                        os.write(buffer, 0, len);
                    }
                } catch (java.io.IOException e) {
                }
                return os.toByteArray();
            }
+1

All Articles