[英]Getting IO exception while creating file in Android?
I am working in an application and getting very silly exception when I create file after creating folder in my SDCARD. 我在应用程序中工作时,在SDCARD中创建文件夹后创建文件时出现非常愚蠢的异常。 I am working with Below code:
我正在使用下面的代码:
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)
I have to create a FOLDER PS inside which I have to create another folder BCREPO inside which I have to create a JPG file. 我必须创建一个文件夹PS,在其中必须创建另一个文件夹BCREPO,在其中必须创建JPG文件。 But getting failed in this.
但是失败了。
It seems you are trying to create two folders "PS/BC_REPO". 似乎您正在尝试创建两个文件夹“ PS / BC_REPO”。 Try using
mkdirs()
instead of mkdir()
and check the return value. 尝试使用
mkdirs()
而不是mkdir()
并检查返回值。 The stacktrace indicates that creating the folder did not work. stacktrace指示创建文件夹无效。
its your directory problem 它是你的目录问题
change "PS/BC_REPO" to "/PS/BC_REPO" 将“ PS / BC_REPO”更改为“ / PS / BC_REPO”
change My_QR_Image.jpg to
/My_QR_Image.jpg
将My_QR_Image.jpg更改为
/My_QR_Image.jpg
Or also you can use the / after the folder name
或者您也可以在文件夹名称后使用/
and check again. 并再次检查。
Environment.getExternalStorageDirectory().toString() Environment.getExternalStorageDirectory()。的toString()
returns /mnt/sdcard so you have to include the
/ ...
返回/ mnt / sdcard,因此您必须包含
/ ...
Try this 尝试这个
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();
}
This is one answer.....Now here we have folder PS in which we have one JPG file. 这是一个答案.....现在,我们在文件夹PS中拥有一个JPG文件。 But i want to create another folder within PS in which I have to store the file.
但是我想在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");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.