Android 读取外部存储上的文件长度,但获取fileNotFoundException

Android 读取外部存储上的文件长度,但获取fileNotFoundException,android,filenotfoundexception,fileinputstream,Android,Filenotfoundexception,Fileinputstream,我有以下代码,可以将FileInputStream创建为手机下载文件夹中的csv文件 我可以记录文件的长度,但它引发了FileNotFoundException 当我输出了正确的长度时,这怎么可能呢 谢谢 String fileName = "Download/file1.csv"; String path = Environment.getExternalStorageDirectory()+"/"+fileName; File file = new File(

我有以下代码,可以将FileInputStream创建为手机下载文件夹中的csv文件

我可以记录文件的长度,但它引发了FileNotFoundException

当我输出了正确的长度时,这怎么可能呢

谢谢

String fileName = "Download/file1.csv";
        String path = Environment.getExternalStorageDirectory()+"/"+fileName;
        File file = new File(path);

        Log.e(TAG, "file length = " + file.length());


        FileInputStream fin = null;
        try {
            // create FileInputStream object
            fin = new FileInputStream(file);

            byte fileContent[] = new byte[(int)file.length()];

            // Reads up to certain bytes of data from this input stream into an array of bytes.
            fin.read(fileContent);
            //create string from byte array
            String s = new String(fileContent);
            Log.e(TAG, "fileData = " + s);
        }
        catch (FileNotFoundException e) {

            Log.e(TAG, "File not found" + e);
        }
        catch (IOException ioe) {

            Log.e(TAG, "Exception while reading file " + ioe);
        }
        finally {
            // close the streams using close method
            try {
                if (fin != null) {
                    fin.close();
                }
            }
            catch (IOException ioe) {
                System.out.println("Error while closing stream: " + ioe);
                Log.e(TAG, "Error while closing stream: " + ioe);
            }
        }


03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/writegactivity:file length=523
03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/writegactivity:File notfoundjava.io.FileNotFoundException:/storage/emulated/0/Download/file1.csv:open失败:EACCES(权限被拒绝)

@Selvin我知道这一点,但我的印象是,如果我以@Selvin为目标,看看AndyGeeky,你有没有检查你的应用程序是否有权限?@Selvin很抱歉,这是我开始的一个新项目,我工作多年的其他项目设置为22,这个设置为25…我忘记检查了。又是我的错误。。对不起,请删除它。。。无论如何,最好添加请求许可:)虽然此代码可以回答问题,但提供有关如何和/或为什么解决问题的附加上下文将提高答案的长期价值。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>



03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/WriteTagActivity: file length = 523
03-02 11:16:21.922 10112-10112/cftagwriter.carefreegroup.com.cftagwriter E/WriteTagActivity: File not foundjava.io.FileNotFoundException: /storage/emulated/0/Download/file1.csv: open failed: EACCES (Permission denied)
//Create folder first
String dirPath = Environment.getExternalStorageDirectory()+"/Download";
        File dir = new File(dirPath);
        dir.mkdir();

//Create File
        File file = new File(dir,"file1.csv");