我的Downloads文件夹中有一个名为“mysimulation.txt”的文件。
Scanner input = new Scanner(System.in);
String filename = "/mysimulation.txt";
try {
input = new Scanner(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + filename));
Toast.makeText(this, "found", Toast.LENGTH_SHORT).show();
} catch (FileNotFoundException e) {
Toast.makeText(this, "not found " + Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), Toast.LENGTH_LONG).show();
}
为什么我的手机在“下载”文件夹中没有读取此文件? 我找到了一个未找到的文件Exception。
答案 0 :(得分:0)
对于任何大于android 6.0(masrshmallow)的android。将以下代码添加到Java代码中以授予读写权限:
File file = new File(path);
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
123);
//Check if the file is writable or readable (.canRead())
Toast.makeText(this, file.canWrite() ? "y" : "no ", Toast.LENGTH_SHORT).show();