Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Wednesday, February 17, 2016

How can I refresh the Gallery after I inserted an Image in android?

How can I refresh the Gallery after I inserted an Image in android?


I've added an inserted in Gallery using android API as following:

Images.Media.insertImage(ctx.getContentResolver(), "scard/test.jpg", "Hello" , "description");

Actually the image that I passed its full path (scard/test.jpg) is already successfully inserted in the DB, but when you open the gallery you can't see it unless you switch off/on the device or Mount/Unmount the external memory.

It there any way to refresh the gallery on demand?

Thanks

Bassel Kh.

Answer by Samuh for How can I refresh the Gallery after I inserted an Image in android?


You have to run a Media Scanner. This might help.

Answer by Derzu for How can I refresh the Gallery after I inserted an Image in android?


The solution using the Media Scanner (sendBroadcast) is very good, and I'm using that in my application.

But, probably, on sdcards with a lot of pics and data, this operation should reach a high processing cust.

There is another solution, after save your media file on gallery, you should advise the the gallery DB that another file was inserted. That can be done like that:

private void addImageGallery( File file ) {      ContentValues values = new ContentValues();      values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());      values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); // setar isso      getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  }  

This code doesn't save your media file to gallery, only saves the information about a new file in the gallery DB. You should store real file before.

This solution is faster because the gallery will not be fully re-scanned. But is not so trustful because all the information about the file was add manually.

Answer by damson for How can I refresh the Gallery after I inserted an Image in android?


static public boolean resetExternalStorageMedia(Context context) {      if (Environment.isExternalStorageEmulated())          return (false);      Uri uri = Uri.parse("file://" + Environment.getExternalStorageDirectory());      Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, uri);        context.sendBroadcast(intent);      return (true);  }    static public void notifyMediaScannerService(Context context, String path) {      MediaScannerConnection.scanFile(context,              new String[] { path }, null,              new MediaScannerConnection.OnScanCompletedListener() {          public void onScanCompleted(String path, Uri uri) {              Log.i("ExternalStorage", "Scanned " + path + ":");              Log.i("ExternalStorage", "-> uri=" + uri);          }      });  }  

Answer by ss1271 for How can I refresh the Gallery after I inserted an Image in android?


I encountered this problem recently, I tried @Samuh's solution, but not works perfectly until I found the solution from Google's example:

   // Tell the media scanner about the new file so that it is      // immediately available to the user.      MediaScannerConnection.scanFile(this,              new String[] { file.toString() }, null,              new MediaScannerConnection.OnScanCompletedListener() {          public void onScanCompleted(String path, Uri uri) {              Log.i("ExternalStorage", "Scanned " + path + ":");              Log.i("ExternalStorage", "-> uri=" + uri);          }      });  

I tried myself and worked flawlessly.

Or, similarly, you might want to take a look at the reference of the MediaScanner Class and someone on StackOverflow asked this question before: Image, saved to sdcard, doesn't appear in Android's Gallery app

Answer by AliSh for How can I refresh the Gallery after I inserted an Image in android?


You can use this for refresh Android Gallery:

public void refreshAndroidGallery(Uri fileUri) {          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {              Intent mediaScanIntent = new Intent(                      Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);              mediaScanIntent.setData(fileUri);              mContext.sendBroadcast(mediaScanIntent);          } else {              mContext.sendBroadcast(new Intent(                      Intent.ACTION_MEDIA_MOUNTED,                      Uri.parse("file://" + Environment.getExternalStorageDirectory())));          }      }  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.