Tuesday, 15 July 2014

android - Save Images to external public storage using Universal Image Downloader? -



android - Save Images to external public storage using Universal Image Downloader? -

i'm attempting save images external sd card using universal image loader. next extremely outdated code fragment author, nostra, here:

download image stream android universal image loader

i'm interested in recreating successfully, when tried, got

this error:

06-21 07:56:22.586 17647-17647/as;dlkfa e/libegl﹕ phone call opengl es api no current context (logged 1 time per thread) 06-21 07:56:27.562 17647-17647/;alsdjf e/viewrootimpl﹕ senduseractionevent() mview == null 06-21 07:56:30.965 17647-17647/;aldjf; e/viewrootimpl﹕ senduseractionevent() mview == null 06-21 07:56:32.737 17647-17647/a;sdjfl e/androidruntime﹕ fatal exception: main java.lang.nullpointerexception @ com.nostra13.universalimageloader.utils.ioutils.copystream(ioutils.java:73) @ com.nostra13.universalimageloader.utils.ioutils.copystream(ioutils.java:50) @ ;a;ldsfkj.mediadisplayactivity.savetogallery(mediadisplayactivity.java:344) @ as;dlkfj.mediadisplayactivity.onmenuitemclick(mediadisplayactivity.java:298) @ android.widget.popupmenu.onmenuitemselected(popupmenu.java:142) @ com.android.internal.view.menu.menubuilder.dispatchmenuitemselected(menubuilder.java:735) @ com.android.internal.view.menu.menuitemimpl.invoke(menuitemimpl.java:152) @ com.android.internal.view.menu.menubuilder.performitemaction(menubuilder.java:874) @ com.android.internal.view.menu.menupopuphelper.onitemclick(menupopuphelper.java:175) @ android.widget.adapterview.performitemclick(adapterview.java:301) @ android.widget.abslistview.performitemclick(abslistview.java:1507) @ android.widget.abslistview$performclick.run(abslistview.java:3336) @ android.os.handler.handlecallback(handler.java:730) @ android.os.handler.dispatchmessage(handler.java:92) @ android.os.looper.loop(looper.java:137) @ android.app.activitythread.main(activitythread.java:5455) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:525) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1187) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:1003) @ dalvik.system.nativestart.main(native method)

i've changed original code this. beingness called on menu item click:

private void savetogallery() { string imageurl = imageurls.get(pager.getcurrentitem()); file path = environment.getexternalstoragepublicdirectory( environment.directory_pictures); file fileforimage = new file(path, imageurl); inputstream sourcestream = null; file cachedimage = imageloader.getinstance().getdiskcache().get(imageurl); if (cachedimage.exists()) { // if image cached uil seek { sourcestream = new fileinputstream(cachedimage); } grab (filenotfoundexception e) { e.printstacktrace(); } } else { // otherwise - download image imagedownloader downloader = new baseimagedownloader(this); seek { sourcestream = downloader.getstream(imageurl, null); } grab (ioexception e) { e.printstacktrace(); } } // todo : utilize try-finally close streams outputstream targetstream = null; seek { targetstream = new fileoutputstream(fileforimage); } grab (filenotfoundexception e) { e.printstacktrace(); } seek { ioutils.copystream(sourcestream, targetstream, null); } grab (ioexception e) { e.printstacktrace(); } seek { targetstream.close(); } grab (ioexception e) { e.printstacktrace(); } seek { sourcestream.close(); } grab (ioexception e) { e.printstacktrace(); } }

line 344 is:

ioutils.copystream(sourcestream, targetstream, null);

read comments fix. basically, using bad characters https**:**

also, here's new code, works, still won't create album app pretty desirable if want users navigate through own albums. i'll maintain begging help coding gods in comments. also, doesn't handle when saving same image yet.

//todo upon uninstall ensure files still there //todo create sure janky stuff don't crash if save same stuff twice private boolean savetogallery() { string imageurl = imageurls.get(pager.getcurrentitem()); int = imageurl.length()-1; while(imageurl.charat(i) != '/') { i--; } string newimageurl = imageurl.substring(i+1); file path = environment.getexternalstoragepublicdirectory(environment.directory_pictures); file fileforimage = new file(path, newimageurl); // if (!fileforimage.mkdirs()) { // log.e("shit", "directory not created"); // } boolean saved = false; inputstream sourcestream = null; file cachedimage = imageloader.getinstance().getdiskcache().get(imageurl); seek { if (cachedimage != null && cachedimage.exists()) { // if image cached uil sourcestream = new fileinputstream(cachedimage); } else { // otherwise - download image imagedownloader downloader = new baseimagedownloader(this); sourcestream = downloader.getstream(imageurl, null); } } grab (ioexception e) { l.e(e); } if (sourcestream != null) { seek { system.out.println(fileforimage.getabsolutepath()); outputstream targetstream = new fileoutputstream(fileforimage); ioutils.copystream(sourcestream, targetstream, null); targetstream.close(); sourcestream.close(); saved = true; } grab (ioexception e) { l.e(e); } } //updates gallery, dunno how though lol, yay sendbroadcast(new intent(intent.action_media_mounted, uri.parse("file://" + environment.getexternalstoragedirectory()))); homecoming saved; }

alright everyone, had move mkdirs() phone call before appending of url file. i'm tired right figure out ordering of things, certainly due that. here's new code. save image album in app if using universal image loader , part of url file name, otherwise, no soup you:

//todo upon uninstall ensure files still there //todo create sure janky stuff don't crash if save same stuff twice private boolean savetogallery() { string imageurl = imageurls.get(pager.getcurrentitem()); int = imageurl.length()-1; while(imageurl.charat(i) != '/') { i--; } string newimageurl = imageurl.substring(i+1); file path = environment.getexternalstoragepublicdirectory(environment.directory_pictures + "/your app name here"); if (!path.mkdirs()) { log.e("shit", "directory not created"); } file fileforimage = new file(path, newimageurl); boolean saved = false; inputstream sourcestream = null; file cachedimage = imageloader.getinstance().getdiskcache().get(imageurl); seek { if (cachedimage != null && cachedimage.exists()) { // if image cached uil sourcestream = new fileinputstream(cachedimage); } else { // otherwise - download image imagedownloader downloader = new baseimagedownloader(this); sourcestream = downloader.getstream(imageurl, null); } } grab (ioexception e) { l.e(e); } if (sourcestream != null) { seek { system.out.println(fileforimage.getabsolutepath()); outputstream targetstream = new fileoutputstream(fileforimage); ioutils.copystream(sourcestream, targetstream, null); targetstream.close(); sourcestream.close(); saved = true; } grab (ioexception e) { l.e(e); } } //updates gallery, dunno how though lol, yay sendbroadcast(new intent(intent.action_media_mounted, uri.parse("file://" + environment.getexternalstoragedirectory()))); homecoming saved; }

user#### pleasure help you. if op's talkative , informing!

android exception-handling try-catch universal-image-loader fileoutputstream

No comments:

Post a Comment