2013-06-29 68 views
3

我在一個應用程序中工作,並且在我的SDCARD中創建文件夾後創建文件時發生非常愚蠢的異常。我用下面的代碼工作:在Android中創建文件時獲取IO異常?

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文件。但在這方面失敗了。

+0

你可以發佈你的stacktrace嗎? – brillenheini

+1

您是否有權限編寫外部存儲目錄? – Blackbelt

+0

是的我有權限。請參閱我已發佈堆棧跟蹤 –

回答

1

看來你正在嘗試創建兩個文件夾「PS/BC_REPO」。嘗試使用mkdirs()而不是mkdir()並檢查返回值。堆棧跟蹤表明創建文件夾不起作用。

+1

也許PS目錄已經存在,他只是創建一個子目錄? –

+0

brillenheini:我已經嘗試過..但仍然獲得expcetion –

+1

@ Sam-In-TechValens如果創建目錄有效,你檢查了布爾返回值嗎? – brillenheini

0

其目錄的問題
變化「PS/BC_REPO」「/ PS/BC_REPO」
變化My_QR_Image.jpg/My_QR_Image.jpg
或者也可以使用/文件夾名稱後

並再次檢查。

Environment.getExternalStorageDirectory()。的toString()
回報到/ mnt/SD卡所以你必須包括/ ...

+0

同樣的結果...仍然得到IOExcetpion –

+0

檢查我更新的答案 – Kuluval

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(); 

    } 
0

這是一個答案.....現在,我們在這裏有我們有一個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"); 
     } 
    }