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


Last Updated:

  1. kadmos

    kadmos Member

    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 :

    Code (Text):
    1. String 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:


    Code (Text):
    1. ZipInputStream 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.

    Advertisement
  2. GeorgeN

    GeorgeN Well-Known Member

    It looks like you might be reading and writing the same data twice.. Do you really need that for loop?

    I'm also not sure if you should be wrapping the ZipInputStream in a BufferedInputStream like that.. Presumably getNextEntry() seeks within the ZipInputStream to the start of the next file. I don't know if the BufferedInputStream will take the stream position from the underlying ZipInputStream, or if it maintains its own position in the stream. Try wrapping the FileInputStream you pass into the ZipInputStream constructor with a BufferedInputStream instead.

    -George
  3. kadmos

    kadmos Member

    Thank you for the response GeorgeN. Someone else also responded to this on another forum and posted the ZipInputStream example code from the Developers Guide. i still had to figure out how to actually write the file from the bytes array but now this works perfectly:

    Code (Text):
    1.  
    2. ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
    3. try {
    4.     ZipEntry ze;
    5.     while ((ze = zis.getNextEntry()) != null) {
    6.                            
    7.         Log.i(DEBUG_TAG, "file: " + getFilesDir() + "/" + ze.getName());
    8.                            
    9.         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    10.         byte[] buffer = new byte[1024];
    11.         int count;
    12.         while ((count = zis.read(buffer)) != -1) {
    13.             baos.write(buffer, 0, count);
    14.         }
    15.         String filename = ze.getName();
    16.         byte[] bytes = baos.toByteArray();
    17.  
    18.             FileOutputStream fout = new FileOutputStream(getFilesDir() + "/" + filename);
    19.         fout.write(bytes);
    20.         fout.close();
    21.         }
    22. } finally {
    23.     zis.close();
    24.     fis.close();                       
    25. }
    26.  


    also my apologies for posting this in the wrong forum - mods please move (or remove) as you see fit.

Share This Page