java - Android: captured image not showing up in gallery (Media Scanner intent not working) -
i have following problem: working on app user can take picture (to attach post) , picture saved external storage. want photo show in pictures gallery , using media scanner intent that, not seem work. following official android developer guide when writing code, don't know what's going wrong.
parts of code:
intent capturing image:
private void dispatchtakepictureintent() { intent takepictureintent = new intent(mediastore.action_image_capture); // ensure there's camera activity handle intent if (takepictureintent.resolveactivity(getactivity().getpackagemanager()) != null) { // create file photo should go file photofile = null; try { photofile = createimagefile(); } catch (ioexception ex) { // error occurred while creating file toast.maketext(getactivity(), ex.getmessage(), toast.length_short).show(); } // continue if file created if (photofile != null) { takepictureintent.putextra(mediastore.extra_output, uri.fromfile(photofile)); startactivityforresult(takepictureintent, request_image_capture); } } }
creating file save image:
private file createimagefile() throws ioexception { // create image file name string timestamp = new simpledateformat("yyyymmdd_hhmmss").format(new date()); string imagefilename = "jpeg_" + timestamp + "_"; file storagedir = environment.getexternalstoragepublicdirectory( environment.directory_pictures); file image = file.createtempfile( imagefilename, /* prefix */ ".jpg", /* suffix */ storagedir /* directory */ ); // save file: path use action_view intents mcurrentphotopath = image.getabsolutepath(); return image; }
displaying image in view:
private void setpic() { // dimensions of view int targetw = 60; int targeth = 100; // dimensions of bitmap bitmapfactory.options bmoptions = new bitmapfactory.options(); bmoptions.injustdecodebounds = true; bitmapfactory.decodefile(mcurrentphotopath, bmoptions); int photow = bmoptions.outwidth; int photoh = bmoptions.outheight; // determine how scale down image int scalefactor = math.min(photow/targetw, photoh/targeth); // decode image file bitmap sized fill view bmoptions.injustdecodebounds = false; bmoptions.insamplesize = scalefactor; bmoptions.inpurgeable = true; bitmap bitmap = bitmapfactory.decodefile(mcurrentphotopath, bmoptions); img_added.setimagebitmap(bitmap); }
broadcasting media scanner intent make image show in gallery:
private void galleryaddpic() { intent mediascanintent = new intent(intent.action_media_scanner_scan_file); file f = new file(mcurrentphotopath); uri contenturi = uri.fromfile(f); mediascanintent.setdata(contenturi); getactivity().sendbroadcast(mediascanintent); }
code run after image capture intent returned:
@override public void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == request_image_capture && resultcode == activity.result_ok) { setpic(); galleryaddpic(); } }
i have tried use intent.action_media_mounted
instead of intent.action_media_scanner_scan_file
, in case got "permission denied" error. when log uri passed intent, file:///storage/emulated/0/pictures/jpeg_20150803_104122_-1534770215.jpg
, should fine. else works (capturing image, saving external storage , displaying in view) except this, don't know what's going wrong. have idea? in advance!
it depends on how device gallery
implement. popular photo apps receive intent.action_media_scanner_scan_file
broadcast listen android media database.
alternative can insert image mediastore
manually @ same time.
public final void notifymediastorescanner(final file file) { try { mediastore.images.media.insertimage(mcontext.getcontentresolver(), file.getabsolutepath(), file.getname(), null); mcontext.sendbroadcast(new intent( intent.action_media_scanner_scan_file, uri.fromfile(file))); } catch (filenotfoundexception e) { e.printstacktrace(); } }
addition, make sure photo not in private
folder, internal storage or write context.mode_private
. otherwise other apps not have permission access file.
Comments
Post a Comment