• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

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

kadmos

Newbie
Jun 18, 2010
11
0
nyc
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:
String imgFile = new File(getFilesDir(), "myImage.jpg").getAbsolutePath();
			
			ImageView myImageView = new ImageView(this); 
			Bitmap bm = null;
			try{
				
				bm = BitmapFactory.decodeFile(imgFile);
				myImageView.setImageBitmap(bm);
				
			} finally{
			
				mainLayout.addView(myImageView);
			}



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:
ZipInputStream zis = new ZipInputStream(fis);
				
		BufferedInputStream in = new BufferedInputStream(zis, 8192);
		
		ZipEntry ze;
			
				
		while ((ze = zis.getNextEntry()) != null){
									
			File dest_file = new File(getFilesDir(), ze.getName());
						
			FileOutputStream fout = new FileOutputStream(getFilesDir() + "/" + ze.getName());

			BufferedOutputStream out = new BufferedOutputStream(fout, 8192);
						 
			byte b[] = new byte[1024];
			int n;
			while ((n = in.read(b,0,1024)) >= 0) {

				out.write(b,0,n);
			}
						 
						 
			for (int c = zis.read(); c != -1; c = zis.read()) { 

				fout.write(c); 
			}
					    
						
			zis.closeEntry();
			fout.close();
					       	
		}
			
	
		zis.close();
		fis.close();



At a terrible standstill here. Appreciate any solutions/suggestions.
 
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
 
Upvote 0
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:
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(fis));
try {
	ZipEntry ze;
	while ((ze = zis.getNextEntry()) != null) {
							
		Log.i(DEBUG_TAG, "file: " + getFilesDir() + "/" + ze.getName());
							
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int count;
		while ((count = zis.read(buffer)) != -1) {
			baos.write(buffer, 0, count);
		}
		String filename = ze.getName();
		byte[] bytes = baos.toByteArray();

	        FileOutputStream fout = new FileOutputStream(getFilesDir() + "/" + filename);
		fout.write(bytes);
		fout.close();
		}
} finally {
	zis.close();
	fis.close();						
}



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

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones