android - Save Images, Display in Gallery App -
i'm working on app receives multiple images via socket. save them, wrote following methods:
public static boolean savetempimagetogallery(context c) { try { fileinputstream fis = c.openfileinput(settings.temp_photo_storage); // create name of file: [date]-[time]-baby final string tfilename = new simpledateformat("dd-mm-yyyy_hh-mm-ss") .format(new date()) + ".png"; string state = environment.getexternalstoragestate(); if (environment.media_mounted.equals(state)) { log.d(tag, "external storage available."); // sd card available file dir = getexternalstoragedir("photos"); if (dir.mkdirs() || dir.isdirectory()) { log.i(tag, "directory: "+dir.getabsolutepath()); file newimage = new file(dir, tfilename); if (newimage.createnewfile() && newimage.isfile()) { log.i(tag, "saving image "+newimage.getabsolutepath()); final bitmap bmp = bitmapfactory.decodestream(fis); bytearrayoutputstream baos = new bytearrayoutputstream(); // compress image png bmp.compress(bitmap.compressformat.png, 40, baos); fileoutputstream fo = new fileoutputstream(newimage); fo.write(baos.tobytearray()); fo.close(); log.i(tag, "image saved!"); return true; } } else { log.d(tag, "could not create directory."); } } else { log.d(tag, "external storage not available."); } } catch (exception e) { } return false; } public static file getexternalstoragedir() { file sdcard = environment.getexternalstoragedirectory(); file dir = new file(sdcard.getabsolutepath() + "/" + settings.ext_storage_directory); return dir; } public static file getexternalstoragedir(string subdir) { file sdcard = environment.getexternalstoragedirectory(); file dir = new file(sdcard.getabsolutepath() + "/" + settings.ext_storage_directory + "/" + subdir); return dir; }
after saving them, i'd offer user possibility view them in default gallery app. after reading post, adapted following code:
mediascannerconnectionclient mscanclient = new mediascannerconnectionclient() { @override public void onscancompleted(string path, uri uri) { try { log.d("onscancompleted", uri + "success"); if (uri != null) { intent intent = new intent(intent.action_view); intent.setdata(uri); startactivity(intent); } } { if (mscancon != null) mscancon.disconnect(); mscancon = null; } } @override public void onmediascannerconnected() { log.i(tag, "media scan connected."); string[] files = support.getexternalstoragedir("photos") .list(); log.i(tag, support.getexternalstoragedir("photos").list().length + " elements in dir."); if (files.length > 0) { (string cur : files) { if (cur.equals(".") || cur.equals("..")) continue; log.i(tag, "using " + cur + " scan stuff. " + support.getexternalstoragedir("photos") .getabsolutepath() + "/" + cur); log.i(tag, "not using " + cur + " scan stuff. " + support.getexternalstoragedir("photos") .tostring() + "/" + cur); mscancon.scanfile( support.getexternalstoragedir("photos") .getabsolutepath() + "/" + cur, "image/*"); break; } } else { toast.maketext(getapplicationcontext(), "no images available.", toast.length_long) .show(); } } }; if (mscancon != null) mscancon.disconnect(); mscancon = new mediascannerconnection(getapplicationcontext(), mscanclient); mscancon.connect();
weird thing: seems onmediascannerconnected never fired - has idea? i've been searching web , stackoverflow last hour..
thank you.
you don't have connect media scanner start scan, can use this static method instead.
mediascannerconnection.scanfile(context, new string[] {dir.getabsolutepath()}, null, null);
edit:
uri uri = uri.parse(filepath); intent = new intent(intent.action_view); i.setdataandtype(uri, "image/*"); startactivity(i);
Comments
Post a Comment