Android: Refreshing the Gallery after saving new images

后端 未结 8 910
天涯浪人
天涯浪人 2020-12-02 19:20

So in my application I at one point save a bunch of images to a temporary folder, and I want them to show up immediately in the Gallery. Off of a reboot, they do, but otherw

8条回答
  •  独厮守ぢ
    2020-12-02 19:54

    Below code work all device for refreshing the gallery after saving image.

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
           Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
           File f1 = new File("file://" + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
           Uri contentUri = Uri.fromFile(f1);
           mediaScanIntent.setData(contentUri);
           sendBroadcast(mediaScanIntent);
       } else {
           sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
       }
       sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("your image path"))));
    

提交回复
热议问题