Reputation: 511
I want to write a file in internal storage in a specific folder, I get error when creating fileoutputStream. Below is my code:
String filePath = ctx.getFilesDir().getPath().toString() +"/"+folderName;
Log.d("filePath",filePath);
file= new File(filePath, filename);
final FileOutputStream fileOutput = new FileOutputStream(file);
Upvotes: 2
Views: 5454
Reputation: 461
you can go this way.
File directory = new File("path_to_directory");
try {
if(!file.exists()) {
directory.createNewFile();
}
File dataFile = new File(directory, "Your File Name");
FileOutputStream stream = new FileOutputStream(dataFile, true); // true if append is required.
stream.write();
stream.flush()
}
catch (IOException e) {
e.printStackTrace();
}
finally {
if (null != stream) {
stream.close();
}
Here path_to_directory = Environment.getExternalStorageDirectory() + File.seperator + "FolderName";
OR
path_to_directory = ctx.getFilesDirectory() + File.seperator + "FolderName";
By the way, you cannot create a folder into android's internal storage until it is rooted. It will definitely give you the IOException because without root the android internal FS is read-only. So be aware of that.
Thanks, Happy Coding :-)
Upvotes: 3
Reputation: 157
okay if you want to save it from volley (or download it and save to specific folder) here the code
DownloadManager.Request request = new DownloadManager.Request(
downloadUri);
request.setAllowedNetworkTypes(
DownloadManager.Request.NETWORK_WIFI
| DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false).setTitle(notesName)
// .setDescription("Something useful. No, really.")
.setDestinationInExternalPublicDir("/YOUR PATH"+"/"+OTHER PATH, notesName)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
Upvotes: 0