1

アプリケーションで作業していて、SDCARD にフォルダーを作成した後にファイルを作成すると、非常にばかげた例外が発生します。私は以下のコードで作業しています:

private void downloadFileFromURL(String filePath){
        String extStorageDirectory = Environment.getExternalStorageDirectory() .toString();
        File folder = new File(extStorageDirectory, "PS/BC_REPO");
        folder.mkdir();
        File file=new File(folder, "My_QR_Image.jpg");
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();

        }
        boolean pdfSize=Downloader.downloadFile(QRCodeImageURL1, file, GetAllDataFromServerActivity.this);

        if(pdfSize){
            System.out.println("downloaded");
        }
    }



java.io.IOException: Not a directory
at java.io.File.createNewFileImpl(Native Method)
at java.io.File.createNewFile(File.java:1160)
t com.tech.persociety.GetAllDataFromServerActivity.downloadFileFromURL(GetAllDataFromServerActivity.java:614)
at com.tech.persociety.GetAllDataFromServerActivity.getJsonResponse(GetAllDataFromServerActivity.java:169)
at com.tech.persociety.GetAllDataFromServerActivity.access$0(GetAllDataFromServerActivity.java:124)
at com.tech.persociety.GetAllDataFromServerActivity$1.dispatchMessage(GetAllDataFromServerActivity.java:108)
at com.tech.persociety.GetAllDataFromServerActivity.serverResponse(GetAllDataFromServerActivity.java:103)
at com.tech.servercommunication.WebServiceCommunicator.notifyRegisteredUser(WebServiceCommunicator.java:225)
at com.tech.servercommunication.WebServiceCommunicator.handleResponse(WebServiceCommunicator.java:211)
at com.tech.servercommunication.WebServiceCommunicator$2.run(WebServiceCommunicator.java:99)
at java.lang.Thread.run(Thread.java:1096)
: W/System.err(6175): java.io.IOException: Not a directory
at java.io.File.createNewFileImpl(Native Method)
at java.io.File.createNewFile(File.java:1160)
at com.tech.persociety.Downloader.downloadFile(Downloader.java:32)
at com.tech.persociety.GetAllDataFromServerActivity.downloadFileFromURL(GetAllDataFromServerActivity.java:623)
at com.tech.persociety.GetAllDataFromServerActivity.getJsonResponse(GetAllDataFromServerActivity.java:169)
at com.tech.persociety.GetAllDataFromServerActivity.access$0(GetAllDataFromServerActivity.java:124)
at com.tech.persociety.GetAllDataFromServerActivity$1.dispatchMessage(GetAllDataFromServerActivity.java:108)
at com.tech.persociety.GetAllDataFromServerActivity.serverResponse(GetAllDataFromServerActivity.java:103)
at com.tech.servercommunication.WebServiceCommunicator.notifyRegisteredUser(WebServiceCommunicator.java:225)
at com.tech.servercommunication.WebServiceCommunicator.handleResponse(WebServiceCommunicator.java:211)
at com.tech.servercommunication.WebServiceCommunicator$2.run(WebServiceCommunicator.java:99)
at java.lang.Thread.run(Thread.java:1096)

FOLDER PS を作成する必要があり、その中に別のフォルダー BCREPO を作成する必要があり、その中に JPG ファイルを作成する必要があります。しかし、これで失敗しました。

4

4 に答える 4

1

「PS/BC_REPO」という 2 つのフォルダを作成しようとしているようです。mkdirs()の代わりに使用しmkdir()て戻り値を確認してください。スタックトレースは、フォルダーの作成が機能しなかったことを示しています。

于 2013-06-29T09:49:30.453 に答える
0

これが 1 つの答えです。ここに、1 つの JPG ファイルを含むフォルダー PS があります。しかし、ファイルを保存する必要がある別のフォルダーを PS 内に作成したいと考えています。

private void downloadFileFromURL(String filePath){
        String QRCodeImageURL = this.getIntent().getStringExtra("QR_CODE"); 
        String extStorageDirectory = Environment.getExternalStorageDirectory() .toString();
        File folder = new File(extStorageDirectory, "BCRepo");
        folder.mkdir();
        File file=new File(folder, "My_QR_Image.jpg");
        try {
            file.createNewFile();
        } catch (IOException e1) {
            e1.printStackTrace();
            runOnUiThread(new Runnable() {
                public void run() {
                    Constant.showAlertDialog(Constant.DIALOG_TITLE_ERROR,"Error", GetAllDataFromServerActivity.this,false);
                }
            });
        }
        boolean pdfSize=Downloader.downloadFile(QRCodeImageURL, file, GetAllDataFromServerActivity.this);
        if(pdfSize){
            System.out.println("downloaded");
        }
    }
于 2013-06-29T10:45:50.407 に答える