BitmapFactory : "decode returned false" for internal image files extracted from zip

  • Replies:4
josh wynn
  • Forum posts: 4

Apr 19, 2012, 3:16:36 PM via Website

Hello again all,

Need to display images downloaded and extracted from a zip into the "/files" directory of the app. the images are getting in there properly as far as i can tell - i am able to extract them from the emulator and view/open them from my desktop. but every attempt, every variation of code i have found and tried so far has failed (Tag: skia / Text: --- decoder->decode returned false).

My latest construct, which does work for image files downloaded separately and uncompressed :

1String imgFile = new File(getFilesDir(), "myImage.jpg").getAbsolutePath();
2
3 ImageView myImageView = new ImageView(this);
4 Bitmap bm = null;
5 try{
6
7 bm = BitmapFactory.decodeFile(imgFile);
8 myImageView.setImageBitmap(bm);
9
10 } finally{
11
12 mainLayout.addView(myImageView);
13 }



And here is the construct i am using to handle the zip extraction. I assume this is where the problem lies but i am clueless as to what i could possibly do differently and to what effect:


1ZipInputStream zis = new ZipInputStream(fis);
2
3 BufferedInputStream in = new BufferedInputStream(zis, 8192);
4
5 ZipEntry ze;
6
7
8 while ((ze = zis.getNextEntry()) != null){
9
10 File dest_file = new File(getFilesDir(), ze.getName());
11
12 FileOutputStream fout = new FileOutputStream(getFilesDir() + "/" + ze.getName());
13
14 BufferedOutputStream out = new BufferedOutputStream(fout, 8192);
15
16 byte b[] = new byte[1024];
17 int n;
18 while ((n = in.read(b,0,1024)) >= 0) {
19
20 out.write(b,0,n);
21 }
22
23
24 for (int c = zis.read(); c != -1; c = zis.read()) {
25
26 fout.write(c);
27 }
28
29
30 zis.closeEntry();
31 fout.close();
32
33 }
34
35
36 zis.close();
37 fis.close();



At a terrible standstill here. Appreciate any solutions/suggestions.

— modified on Apr 19, 2012, 3:21:55 PM

Reply
Jeremiah
  • Forum posts: 775

Apr 20, 2012, 4:32:04 AM via Website

Not sure, but I think the problem is that your creating a buffered input stream from the ZipInputStream. I think it should be the other way around, and create a ZipInputStream from the buffered input stream. The example below is taken from the developers guide, creates the zipinputstream from the buffered input stream:

1InputStream is = ...
2 ZipInputStream zis = new ZipInputStream(new BufferedInputStream(is));
3 try {
4 ZipEntry ze;
5 while ((ze = zis.getNextEntry()) != null) {
6 ByteArrayOutputStream baos = new ByteArrayOutputStream();
7 byte[] buffer = new byte[1024];
8 int count;
9 while ((count = zis.read(buffer)) != -1) {
10 baos.write(buffer, 0, count);
11 }
12 String filename = ze.getName();
13 byte[] bytes = baos.toByteArray();
14 // do something with 'filename' and 'bytes'...
15 }
16 } finally {
17 zis.close();
18 }

josh wynn

Reply
josh wynn
  • Forum posts: 4

Apr 20, 2012, 9:33:09 AM via Website

you rock Jeremiah thank you again. still had to figure out exactly how to write the file from the byte array but that wasnt hard to find once i knew what to look for. works perfectly now.

but since we are on the subject - would you happen to know if the new APK Expansion Zip Library is the only way to read data from a zip without having to bother with extraction? is there a way to do it that doesnt involve using expansion files?

— modified on Apr 20, 2012, 9:36:14 AM

Reply
Jeremiah
  • Forum posts: 775

Apr 23, 2012, 8:10:34 PM via Website

I don't know what you mean by new APK Expansion Zip Library? java.util.Zip is standard part of android api, since api level 1. What expansion files are you needing?

Reply
Eric McBride
  • Forum posts: 1,790

Apr 25, 2012, 4:03:45 PM via Website

Yes..Jeremiah does indeed rock :grin:

Reply