Search code examples
androidandroid-7.0-nougatsecurityexception

How can I update the File System after creating a folder programmatically?


I already created a folder in android grammatically but it doesn't show in File Explorer while device connected through USB cable.

Below is a piece of source code that i used to scan File system but it doesn't work. It giving error permission denial (Security Exception)

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                    context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                            + Environment.getExternalStorageDirectory())));
                    MediaScannerConnection.scanFile(context, new String[]{new File(Environment.getExternalStorageDirectory().toString()).getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() {
                        @Override
                        public void onScanCompleted(String path, Uri uri) {

                        }
                    });
                }

I'm using Android device that has Android 7.0 (OS). Can you help me out from this problem?


Solution

  • Try:

    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
                        + Environment.getExternalStorageDirectory())));
    
            } else {
                MediaScannerConnection.scanFile(context, new String[]{new File(Environment.getExternalStorageDirectory().toString()).getAbsolutePath()}, null, new MediaScannerConnection.OnScanCompletedListener() {
                    @Override
                    public void onScanCompleted(String path, Uri uri) {
                      //No need to call anything here
                    }
                });
            }