2012-04-20 3 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 '을 사용하여 에뮬레이터 또는 장치에서 파일을 가져올 수 있습니다. 그런 다음 정확히 서버에 있는지 여부를 확인할 수 있습니다. –

+0

나는 그것을 에뮬레이터에서 떼어 냈다. 어떻게 든 중요하다면 이미지는 13kB입니다. –

+0

'Movie.decodeFile (새 파일 (context.getCacheDir(), "mygif.gif"). getAbsolutePath())'를 사용해 보셨습니까? –

답변

13

제안하십시오 cache 제외하고 다른 폴더를 sdcard, 또는 응용 프로그램 :

이 같은 다른 폴더로 다운로드 위치를 이동 1.try. inputstream 초기화

할수 있도록 팝업을 사용하려고이 같은 코드를 변경 해달라고 사용 :

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

3.to 파일 폴더

업데이트에 존재 확인 버퍼 크기 init을 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가 null이 아닙니다.

또한 코드에서 예외가 발생하지만 movie도 null이 아닙니다. 내가 아는

+0

1. 차이 없음. 2. 차이점 없음. 3. 존재하고 부패하지 않습니다 –

+0

답을 업데이트하십시오. 확인하십시오. – idiottiger

+0

감사합니다. 아래 코드는 작업을 수행합니다! mark()가 누락되었습니다. 고마워요 –

2

는 정말 오래된 게시물입니다,하지만 난 내 대답은 심지어 삼성 장치

0

에, 바이트 배열에의 InputStream를 복사하고 Movie.decodeByteArray이 나를 위해 일한 호출 같은 문제

을 가졌다 BeNeXuS의 대답을 기반으로합니다.

파일 크기는 항상 알려진되지 않을 수 있습니다

, 참조 :

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

또는

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

을 inputStream을받은 후 : 4.4 최대

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

안드로이드는 동영상을 디코딩 실패 InputStream.reset() 메서드에서 내부 IOException이 발생하는 경우 파일 및 스트림에서 가져옵니다. 그러나 바이트에서 디코딩은 모든 버전의 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);