2017-10-05 2 views
0

먼저 단순한 오류가 발생하면 사과드립니다. 나는 대부분 스스로 독학을하고, 내가 아는 것은 믿을 수 없을 정도로 산발적이며 우연이 될 수 있습니다.파일을 가져온 후 파일을 찾을 수 없습니다.

저는 암호 관리자로 작동하는 앱을 만들고 있습니다. 그것은 백업 및 복원 기능이 있습니다. 문제없이 고정 된 디렉토리를 사용하여 작업 할 수있게되었지만 파일 선택기를 추가 할 때 문제가있었습니다.

파일 위치가 문제없이 저장됩니다. 그런 다음 한 줄에 파일이 저장되었음을 확인한 후 파일이 존재하지 않는다고 표시됩니다. 오류 로그뿐만 아니라 관련 코드도 있습니다. 나는 충돌에 코드를 포함시킬 것인데, 그 뒤에 오는 것은 부적절하지만 질문을 받으면 제공 될 수 있습니다. PSync.psbk는 읽으려고하는 파일입니다. 솔직히 뭘 잘못했는지는 모르겠지만 간단하다고 가정합니다.

코드 :

Toast.makeText(Main.this, "Method properly called.", Toast.LENGTH_SHORT).show(); 
       //The file variable to imported. 
       File file; 

       try { 
        //Sets the file equal to the file found at the specified path. 
        //Used to access settings. 
        TinyDB database = new TinyDB(getApplicationContext()); 

        String strfilePath = database.getString("FilePath"); 
        Toast.makeText(Main.this, "Method properly called: " + strfilePath, Toast.LENGTH_SHORT).show(); 
        file = new File(strfilePath); 

        //To be used to arrange the imported information. 
        ArrayList<String> strAcc = new ArrayList<>(); 
        ArrayList<String> strUser = new ArrayList<>(); 
        ArrayList<String> strPass = new ArrayList<>(); 
        ArrayList<String> strAdditionalInfo = new ArrayList<>(); 

        //To be used to store all the information for additional info variables. This is 
        //due to its multi-line nature requiring a slightly different method of 
        //importation, the other variables are expected to be one line. 
        String strExtraInfo = ""; 

        //Goes through the file and adds info to arrays for each corresponding variable. 
        //If the line does not have an identifier, it assumes it to be an additional 
        //info line, and will be processed later. 
        try (BufferedReader br = new BufferedReader(new FileReader(file))) { //Line 776, as mentioned in err log. 

ERR 로그 :

W/System.err: java.io.FileNotFoundException: /document/storage/emulated/0/Download/PSync.psbk (No such file or directory) 
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:  at java.io.FileInputStream.open(Native Method) 
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:  at java.io.FileInputStream.<init>(FileInputStream.java:146) 
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:  at java.io.FileReader.<init>(FileReader.java:72) 
    10-04 21:16:32.843 19405-19405/com.example.brand.psync W/System.err:  at com.example.brand.psync.Main.onRequestPermissionsResult(Main.java:776) 
+0

매니페스트에서 허가를 받았습니다 –

+0

예, 가지고 있으며 런타임 권한도 요청합니다. file = new File (Environment.getExternalStoragePublicDirectory (Environment.DIRECTORY_DOWNLOADS), "PSync.psbk") 형식의 파일을 읽는 데 아무런 문제가 없습니다. – Prometheus

+0

@NabinBhandari 예,이 코드 스 니펫은 onRequestPermissionsResult 안에 있습니다. 코드는 권한이 부여 된 후에 만 ​​실행됩니다. Amrutha에게 말했듯이, Environmemnt.get 메소드를 사용할 때 동일한 디렉토리에서 같은 파일을 읽는 데 문제가 없습니다. – Prometheus

답변

2

액세스하려고하는 경로는 다음과 같습니다

/문서/저장/에뮬레이션/0/다운로드/PSYNC .psbk

경로에 /문서을 이어서는 안됩니다. 경로를 저장하는 동안 문제가있을 수 있습니다.

+0

테스트하기 전에 명확히하기 위해 경로가 어떻게 생겼습니까? /storage/emulated/0/Download/PSync.psbk? – Prometheus

+0

네, 맞습니다. –

+0

당신이 그렇게 말한 후에, 나는 그것이 이상하다고 생각했기 때문에 뭔가를 시험하기로 결정했습니다. 나는 의도를 방송하고 파일 관리자를 사용하게하여 파일 경로를 얻습니다. 이상한 이유로 내 주 파일 브라우저에서 "/document/storage/emulated/0/Download/PSync.psbk"로 반환했지만 다른 파일 브라우저를 사용하면 올바른 경로가 반환되어 작동 할 수있었습니다. 이 코드에서 오류가 아니기 때문에이 질문에 어떻게 대답해야합니까? 네가 바로 확인해? 아니면 코드와 관련이 없으므로 삭제합니까? – Prometheus

관련 문제