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)

我必须在其中创建一个文件夹 PS,我必须在其中创建另一个文件夹 BCREPO,我必须在其中创建一个 JPG 文件。但是在这方面失败了。

4

4 回答 4

1

您似乎正在尝试创建两个文件夹“PS/BC_REPO”。尝试使用mkdirs()代替mkdir()并检查返回值。堆栈跟踪表明创建文件夹不起作用。

于 2013-06-29T09:49:30.453 回答
0

您的目录问题
“PS/BC_REPO”更改为“/PS/BC_REPO”
My_QR_Image.jpg更改为/My_QR_Image.jpg
或者您也可以在文件夹名称后使用/

并再次检查。

Environment.getExternalStorageDirectory().toString()
返回/mnt/sdcard所以你必须包括/ ...

于 2013-06-29T10:14:50.277 回答
0

这是一个答案.....现在我们有一个文件夹 PS,其中有一个 JPG 文件。但我想在 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 回答
0

尝试这个

File mSampleFile = null;
  if (mSampleFile == null) {

   String newFolder = "/PS/BC_REPO";

        String extStorageDirectory = Environment
                .getExternalStorageDirectory().toString();

    File myNewFolder = new File(extStorageDirectory + newFolder);

    if (!myNewFolder.exists()) {

    myNewFolder.mkdir();

    }
于 2013-06-29T10:22:57.103 回答