android - Saving multiple jpgs to sd card -
i downloading images parse.com. never know amount of images have using counter retrieve them. reason, counter gets 5, , downloads image 5 times. in storage, have 1 image. here code:
string root = environment.getexternalstoragedirectory().tostring(); int = 0;
retrieve images:
parsequery<parseobject> query = parsequery.getquery("fightgallery"); query.findinbackground(new findcallback<parseobject>() { @override public void done(list<parseobject> parseobjects, com.parse.parseexception e) { if (e == null) { int size = parseobjects.size(); log.d("query size", "size " + size + " int " + i); while (i < size) { parsefile fileobject = parseobjects.get(i).getparsefile("image"); fileobject.getdatainbackground(new getdatacallback() { @override public void done(byte[] bytes, parseexception e) { if (e == null) { log.d("data", "we have data " +i); bitmap bmp = bitmapfactory.decodebytearray(bytes, 0, bytes.length); saveimagesinsdcard(bmp, i); } else { log.d("error: ", "" + e.getmessage()); progressdialog.dismiss(); } } }); i++; } } else { log.d("error:", "" + e.getmessage()); progressdialog.dismiss(); } } });
this saveimagesinsdcard()
private void saveimagesinsdcard(bitmap bmp, int i) { file mydir = new file(root + "/clash_images"); mydir.mkdirs(); string fname = "image"; file file = new file(mydir, fname+i+".jpg"); // if (file.exists()) file.delete(); log.d("image", "saved " +fname + i); try { fileoutputstream out = new fileoutputstream(file); bmp.compress(bitmap.compressformat.png, 90, out); out.flush(); out.close(); } catch (filenotfoundexception e1) { e1.printstacktrace(); } catch (ioexception e2) { e2.printstacktrace(); } }
here logcat showing log.d
08-07 12:54:45.586 10403-10403/com.codealchemist.clashmma d/query size: size 5 int 0 08-07 12:54:46.407 10403-10403/com.codealchemist.clashmma d/data: have data 5 08-07 12:54:46.527 10403-10403/com.codealchemist.clashmma d/image: saved image5 08-07 12:54:48.079 10403-10403/com.codealchemist.clashmma i/choreographer: skipped 100 frames! application may doing work on main thread. 08-07 12:54:48.089 10403-10403/com.codealchemist.clashmma d/data: have data 5 08-07 12:54:48.199 10403-10405/com.codealchemist.clashmma d/dalvikvm: gc_concurrent freed 8524k, 20% free 38799k/48391k, paused 13ms+4ms, total 60ms 08-07 12:54:48.199 10403-10403/com.codealchemist.clashmma d/image: saved image5 08-07 12:54:50.131 10403-10403/com.codealchemist.clashmma d/data: have data 5 08-07 12:54:50.181 10403-10403/com.codealchemist.clashmma d/image: saved image5 08-07 12:54:51.242 10403-10403/com.codealchemist.clashmma d/data: have data 5 08-07 12:54:51.312 10403-10403/com.codealchemist.clashmma d/image: saved image5 08-07 12:54:53.494 10403-10403/com.codealchemist.clashmma i/choreographer: skipped 323 frames! application may doing work on main thread.
as can see, in logcat, i
never changes 5. how structure download 5 images?
when getdatacallback
executed after file downloaded, loop has run 5 times , such value of i
5 every time.
Comments
Post a Comment