[英]Why isn't my android reading this file in Downloads folder?
I have a file named "mysimulation.txt" in my Downloads folder. 我的下载文件夹中有一个名为“ 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();
}
Why isn't my phone reading this file in the Downloads folder? 为什么我的手机无法读取“下载”文件夹中的该文件? I get a file not found Exception.
我收到未找到文件的异常。
For any android greater than android 6.0 (masrshmallow). 对于任何大于android 6.0(masshmallow)的android。 Add the following code to your java code to grant read and write permissions:
将以下代码添加到您的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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.