android - Use MediaScannerConnection scanFile To Scan Downloaded Image File -


i working on app in save image(s) directory images wont show in gallery until restart phone.

here's code snippet

public class savetask extends asynctask<string , string , string>         {             private context context;             private progressdialog pdialog;             string image_url;             url myfileurl;             string myfileurl1;             bitmap bmimg = null;             file file ;              public savetask(context context) {                 this.context = context;             }              @override             protected void onpreexecute() {                 // todo auto-generated method stub                  super.onpreexecute();                  pdialog = new progressdialog(context);                 pdialog.setmessage("downloading image ...");                 pdialog.setindeterminate(false);                 pdialog.setcancelable(false);                 pdialog.show();              }              @override             protected string doinbackground(string... args) {                 // todo auto-generated method stub                  try {                        myfileurl = new url(args[0]);                     //myfileurl1 = args[0];                      httpurlconnection conn = (httpurlconnection) myfileurl.openconnection();                        conn.setdoinput(true);                        conn.connect();                          inputstream = conn.getinputstream();                     bmimg = bitmapfactory.decodestream(is);                  }                 catch (ioexception e)                 {                            e.printstacktrace();                   }                 try {                             string path = myfileurl.getpath();                     string idstr = path.substring(path.lastindexof('/') + 1);                     file filepath = environment.getexternalstoragedirectory();                     file dir = new file (filepath.getabsolutepath() + "/mydownloaddir/");                     dir.mkdirs();                     string filename = idstr;                     file = new file(dir, filename);                     fileoutputstream fos = new fileoutputstream(file);                     bmimg.compress(compressformat.jpeg, 75, fos);                        fos.flush();                         fos.close();                      }                 catch (exception e)                         {                             e.printstacktrace();                           }                 return null;                }               @override             protected void onpostexecute(string args) {                 // todo auto-generated method stub                 toast.maketext(slideimageactivity.this, "image saved succesfully folder 'mydownloaddir'", toast.length_short).show();                 pdialog.dismiss();             }         } 

what code should use next make images showup in gallery using medisscanner

i got on here unable use properly:

mediascannerconnection.scanfile(applicationcontext.context, new string[] { imagefile.getpath() }, null,           new mediascannerconnection.onscancompletedlistener() {             @override             public void onscancompleted(string path, uri uri) {               log.i(tag, "scanned " + path);             }           }); 

please help

use

context.sendbroadcast(new intent(intent.action_media_mounted, uri.parse("file://"             + environment.getexternalstoragedirectory())));  

//but works on api <19

second method

private void scanfile(string path) {          mediascannerconnection.scanfile(mainactivity.this,                 new string[] { path }, null,                 new mediascannerconnection.onscancompletedlistener() {                      public void onscancompleted(string path, uri uri) {                         log.i("tag", "finished scanning " + path);                     }                 });     } 

call like

scanfile(yourfile.getabsolutepath()); 

also see answer


Comments

Popular posts from this blog

c++ - QTextObjectInterface with Qml TextEdit (QQuickTextEdit) -

javascript - angular ng-required radio button not toggling required off in firefox 33, OK in chrome -

xcode - Swift Playground - Files are not readable -