5

我正在尝试使用android.graphics.Movie/sdcard/download.

如果我将文件放在drawableAPK 的文件夹中,我可以使用以下方法加载它:

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

这样可行。movie.duration()将显示正确的持续时间,我用它来推导movie.setTime().

如果我尝试使用从 sd 卡加载它而不是 drawable,则会出现问题

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

它似乎加载了一些不为movie空的东西。但问题是movie.duration()返回0

知道为什么会发生这种情况,我应该怎么做?

4

3 回答 3

3

我也遇到了这个。经过多次试验和错误后,我使用 getContentResolver().openInputStream 和 android BitmapDecode 示例中的死 streamToBytes 代码使其工作,这似乎有效。我还没有解释为什么需要对包资源和 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();
}
于 2011-08-17T17:01:28.500 回答
2

你是对的,除了一个。您不应该使用 Uri 和 ContentResolver。简短的解决方案是您应该使用

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

代替

Movie.decodeFile(path);

线索是在 decodeFile(path) 方法中创建的某些流的 reset() 方法实现中。

于 2011-09-01T03:15:54.660 回答
1
 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();
            }
于 2012-08-21T10:37:20.943 回答