2017-10-05 6 views
0

最初は、これが単純なエラーである場合はお詫びしたいと思います。私はほとんど独学で、私が知っていることは信じられないほど散発的で、うんざりする可能性があります。ファイルが選択された後にファイルが見つかりません

私はパスワードマネージャとして機能するアプリを開発中です。それはバックアップと復元機能を持っています。固定ディレクトリを使って問題なく動作させることができましたが、ファイルピッカーを追加しようとすると問題が発生しました。

ファイルの場所は問題なく保存されます。次に、保存されたことを確認した後の1行に、ファイルが存在しないことが示されます。関連するコードとエラーログがここにあります。私は、クラッシュにコードを含めるだけで、それに続くのは無関係ですが、尋ねられれば提供することができます。 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

関連する問題