IOException - unable to load file

I have an application that displays GIF images. Everything works fine if the image is saved in drawable, and I refer to it as follows

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

But I need to download the image from the Internet, so I save it to CacheDir, this works fine. I tried to read this.

  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 one

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

But no matter what ends on

 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 this is a very simple mistake, but I cannot overcome it. The manifest has:

 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+6
source share
4 answers

sentence:

1.try move the download location to another folder, for example: sdcard , or to a folder other than cache .

2. when starting inputstream try using: init with buffer size

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

3. check the file in the folder

update: change code like this and don't 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); 

exception thrown, and movie not null.

Also, although your code throws an exception, movie also not null.

+13
source

I know this is a really old post, but I had the same problem.

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

+2
source

My answer is based on BeNeXuS answer.

File size may not always be known, see

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

or

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

After receiving 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
source

Android up to 4.4 cannot decode a movie from a file and stream in case of an internal IOException in the InputStream.reset () method. But byte decoding works well on all versions of Android. A minimal version of the code is enough for this:

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

Source: https://habr.com/ru/post/913689/


All Articles