2012-04-20 100 views
6

我有一個顯示GIF圖像的應用程序。 這一切工作正常,如果圖像保存在繪製,我訪問它像這樣IOException - 無法加載文件

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

但我需要從互聯網上下載圖像,所以我將其保存到CacheDir,工作正常。我嘗試閱讀以下內容。

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); 

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

但無論怎樣,它

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) 

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

結束,我認爲這是一個非常簡單的錯誤,但我可以沒辦法克服它。清單有:

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

你應該測試下載和保存部分是否正在破壞文件。在應用程序下載文件後,您可以使用'adb pull '將其從仿真器或設備中拉出。然後你可以檢查它是否在服務器上。 – 2012-04-20 04:03:33

+0

我把它從仿真器中拉出來,並沒有問題。圖像是13kB,如果不知何故很重要 – 2012-04-20 04:14:45

+0

你試過嗎?Movie.decodeFile(new File(context.getCacheDir(),「mygif.gif」)。getAbsolutePath())'? – 2012-04-20 04:19:54

回答

13

建議:

1.try移動下載位置到另一個文件夾,如:sdcard,或應用程序的另一個文件夾,除了cache

2.當初始化的inputstream,嘗試使用:初始化一個緩衝區大小

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

3.to檢查文件的文件夾

更新的存在:改變這樣的代碼和不使用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); 

沒有異常拋出,而movie不爲空。

另外,雖然您的代碼會拋出異常,但movie也不爲空。

+0

1.沒有區別;沒有區別; 3.它存在且沒有損壞 – 2012-04-20 06:30:06

+0

更新答案,請檢查。 – idiottiger 2012-04-20 06:53:36

+0

謝謝,下面的代碼完成這項工作! mark()丟失。非常感謝 – 2012-04-20 07:07:38

2

我知道是真的老了後,但我有同樣的問題

複製的InputStream字節數組,並呼籲Movie.decodeByteArray工作對我來說,即使是在三星設備

0

我的回答是基於BeNexuS的回答。

文件的大小可能並不總是已知,請參閱:

File file = ...; 
InputStream inputStream = new FileInputStream(file); 

InputStream inputStream = getContext().getContentResolver().openInputStream(uri); 

得到InputStream的後:

BufferedInputStream bis = new BufferedInputStream(inputStream); 
ByteArrayOutputStream baos = new ByteArrayOutputStream(); 

byte buffer[] = new byte[1024]; 
int len; 
while ((len = bis.read(buffer, 0, buffer.length)) > 0) { 
    baos.write(buffer, 0, len); 
} 
bis.close(); 

byte[] imageData = baos.toByteArray(); 
baos.close(); 

Bitmap bitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length); 
0

的Android高達4.4未能電影解碼如果InputStream.reset()方法中存在內部IOException,則從File和Stream處理。但是從字節解碼適用於所有版本的Android。這裏是相當小的版本代碼來做到這一點:

int size = (int) imageFile.length(); 
byte[] buffer = new byte[size]; 
FileInputStream inputStream = new FileInputStream(imageFile); 
int readLength = inputStream.read(buffer); 
inputStream.close(); 
Movie movie = Movie.decodeByteArray(buffer, 0, readLength);