我已经在语法上用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设备。你能解决这个问题吗?
答案 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
}
});
}