Rhidoy Rhidoy - 3 months ago 21
Android Question

How Sync android galley when capture a new image which save to custom folder

i implements an app which capture image and save them to my custom folder. everything is OK but the problem is, my gallery don't get sync when i capture image. it means gallery don't show new capture image. for that i need to reboot my phone.

Here is my image capture code....

if (requestCode == 0 && resultCode == RESULT_OK) {
String[] projection = {MediaStore.Images.ImageColumns.SIZE,
MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATA,
BaseColumns._ID,
MediaStore.Images.ImageColumns.DATE_ADDED};
final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
final String selection = MediaStore.Images.Media.DATE_TAKEN + " > " + time;
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
CursorLoader loader = new CursorLoader(this, u,
projection, selection, null, imageOrderBy);
Cursor cursor = loader.loadInBackground();
if (null != cursor && cursor.moveToFirst()) {
ContentResolver cr = this.getContentResolver();
cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
BaseColumns._ID + "=" + cursor.getString(3), null);
}
//showing image in image view
cameraPicture = output.getPath();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
Bitmap bitmap = BitmapFactory.decodeFile(cameraPicture, options);
binding.imageView.setImageBitmap(bitmap);
binding.imageView.setVisibility(View.VISIBLE);
}



so how can i sync my android galley when i capture the image??? so that i can view this image immediately from galley.

Answer

You need notify system after saving file

 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);
                }
            });
}