如何以编程方式创建文件夹后更新文件系统?

时间:2018-05-21 11:11:55

标签: android android-7.0-nougat securityexception

我已经在语法上用android创建了一个文件夹,但是当设备通过USB线连接时,它不会显示在文件资源管理器中。

下面是我用来扫描文件系统的一段源代码,但它不起作用。它给出了错误权限拒绝(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) {

                        }
                    });
                }

我正在使用Android 7.0(OS)的Android设备。你能解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

尝试:

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