save multiple images from url into internal storage in android -



save multiple images from url into internal storage in android -

i using method images url , downloading more 1 image varable below called "name" array of names of images .i want able store images whos name in array thats why kept url that.it seems work have having problem selecting 1 image out or them.

this code save images

string filename="code"; seek { url url = new url("http://10.0.2.2/picure/"+name+".jpg"); httpurlconnection conn = (httpurlconnection) url.openconnection(); conn.setdoinput(true); conn.connect(); inputstream = conn.getinputstream(); bitmap bm = bitmapfactory.decodestream(is); fileoutputstream fos = getactivity().openfileoutput(filename, context.mode_private); bytearrayoutputstream outstream = new bytearrayoutputstream(); bm.compress(bitmap.compressformat.jpeg, 100, outstream); byte[] bytearray = outstream.tobytearray(); fos.write(bytearray); fos.close(); toast.maketext(getactivity()," connected", toast.length_long).show(); } catch(exception e) { }

this code collect images

string path = mcontext.getfilesdir().tostring(); string filename = "code";

if (filename != null && !filename.equals("")) { bitmap bmap = bitmapfactory.decodefile(path + "/" + filename); if (bmap != null) { category_logo.setimagebitmap(bmap); } }

i know names of images saved how select 1 specifically

for images utilize asynctask, code can download images in cache directory of app:

class imagedownloader extends asynctask<string, void, file> { string imageurl; string name; context ctx; public imagedownloader(context context, string url, string filename) { this.imageurl = url; this.name = filename; this.ctx = context; } @override protected file doinbackground(string... urls) { bitmap micon; file cachedir = ctx.getcachedir(); file f = new file(cachedir, name); seek { inputstream in = new java.net.url(imageurl).openstream(); micon = bitmapfactory.decodestream(in); seek { fileoutputstream out = new fileoutputstream( f); micon.compress( bitmap.compressformat.jpeg, 100, out); out.flush(); out.close(); homecoming f; } grab (filenotfoundexception e) { homecoming null; } grab (ioexception e) { homecoming null; } } grab (exception e) { homecoming null; } } @override protected void onpostexecute(file result) { super.onpostexecute(result); toast.maketext(ctx," connected " + name, toast.length_long).show(); } } }

for phone call asynctask, need utilize for names , url of image:

new imagedownloader(getbasecontext(),url[i],name[i]).execute();

you can edit doinbackground code, httpconnection utilize deprecated in api 22, please utilize illustration above, can alter directory.

and sorry code, can reformat later.

android image internal-storage

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -