I hope your code to write data in file is perfect I am just Concentrate in your problem.I think there is two possibility to get File not found Exception.
- Some time File Name is not Support Some Special Character While you provide this kind of character to giving file name at that time file is not properly Generated that's why this error was occur So please Make sure your file name is Proper.
- second is Your file extension.here your not specified which kind of file you have.
here I have Some Code Snippet I hope this will helping you to Solve problem.
Solution
File file=new File(Environment.getExternalStorageDirectory()+"/"+ConstansClass.FOLDERNAME);
if(!file.exists())
{
file.mkdir();
}
String file=filename.replaceAll("[^a-zA-Z0-9.-]", " ");
yourDir = new File(file, file+".mp3");
if(yourDir.exists())
{
return yourDir;
}
Edit
I am Sorry lannf I don't know what exactly happen in your code but i have one code snippet which is just download mp3 file from soundcloude and Store in my local storage.i think it's exactly same as your requirement.so i have post my code below.
@Override
protected File doInBackground(Void... params) {
//File sdCardRoot = Environment.getExternalStorageDirectory()+"/Music";
File file=new File(Environment.getExternalStorageDirectory()+"/"+ConstansClass.FOLDERNAME);
if(!file.exists())
{
file.mkdir();
}
String filename=soundCloudeResponse.getTitle();//.replaceAll("[^a-zA-Z0-9.-]", " ");
yourDir = new File(file, filename+".mp3");
if(yourDir.exists())
{
return yourDir;
}
String url=soundCloudeResponse.getStream_url()+"?"+ConstansClass.SOUNDCLOUDE_CLIENT_ID;
URL u = null;
try {
DebugLog.e("Request Url"+ url);
u = new URL(url);
URLConnection conn = u.openConnection();
int contentLength = conn.getContentLength();
DataInputStream stream = new DataInputStream(u.openStream());
byte[] buffer = new byte[contentLength];
stream.readFully(buffer);
stream.close();
DataOutputStream fos = new DataOutputStream(new FileOutputStream(yourDir));
fos.write(buffer);
fos.flush();
fos.close();
DebugLog.d("Download Complete in On Background");
} catch (MalformedURLException e) {
sucess=false;
e.printStackTrace();
} catch (IOException e) {
sucess=false;
e.printStackTrace();
}
catch (Exception e)
{
sucess=false;
e.printStackTrace();
DebugLog.e("Error ::"+e.getMessage());
}
return yourDir;
}
I think your are intelligent developer so you can modify this code according to your requirement.
Best Of Luck