1

I am creating a directory to store the bitmap images.This is my code:

 String root = Environment.getExternalStorageDirectory().toString();
                File myDir = new File(root + "/ABC");
                myDir.mkdirs();
                Random generator = new Random();
                int n = 10000;
                n = generator.nextInt(n);
                String fname = "Image-" + n + ".jpg";
                File file = new File(myDir, fname);
                Log.i(TAG, "" + file);
                if (file.exists())
                    file.delete();
                try {
                    FileOutputStream out = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
                    out.flush();
                    out.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

But this folder is not shown in gallery.I am using Android "Marshmallow" version.Can anyone help me?

Amshu
  • 127
  • 10
  • 1
    sometime gallery is not refresh newly creat folder's so try to restart your device... – Ashish Sep 12 '16 at 06:33
  • @Ashish If I reboot it works.But every time I can't do this.Is there any other solution? – Amshu Sep 12 '16 at 06:38
  • turn flight mode on and off also can solve this problem .. – Ashish Sep 12 '16 at 06:41
  • @Ashish when I give this app to user it will create problem if I do this. – Amshu Sep 12 '16 at 06:54
  • You should invoke the media scanner for the new file. One line of code. Please google for it. – greenapps Sep 12 '16 at 06:55
  • as greenapps say use media scanner. i refer a link for your answer use this. – Ashish Sep 12 '16 at 07:01
  • @Ashish sendBroadcast ( new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())) ); I have added this line line im my code inside onCreate().Then also it is not refreshing. – Amshu Sep 12 '16 at 07:03
  • no it works.. may be you use device below kitkat so this create problem for you. i update my answer please check. – Ashish Sep 12 '16 at 07:07

1 Answers1

1

Use this broadcast

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    Intent mediaScanIntent = new Intent(
                            Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                    Uri contentUri = Uri.fromFile(out); //out is your file you saved/deleted/moved/copied
                    mediaScanIntent.setData(contentUri);
                    this.sendBroadcast(mediaScanIntent);
                } else {
                    sendBroadcast(new Intent(
                            Intent.ACTION_MEDIA_MOUNTED,
                            Uri.parse("file://"
                                    + Environment.getExternalStorageDirectory())));
                }

refer this link : link to help

Community
  • 1
  • 1
Ashish
  • 371
  • 3
  • 18