Reputation: 87
I've a next problem... I searched answer on the source, but didn't find a good answer...
java.io.FileNotFoundException: /mounted/EmailClient/side-corner.png: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:453)
at java.io.FileOutputStream.<init>(FileOutputStream.
at java.io.FileOutputStream.<init>(FileOutputStream.java:73) etc
Code:
private static String saveFile(String filename, InputStream input) {
String path = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(path)) {
try {
byte[] attachment = new byte[input.available()];
input.read(attachment);
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/EmailClient/", filename);
if (!file.mkdirs())
Log.d("EmailClient", "saveFile: Dir not created");
FileOutputStream out = new FileOutputStream(file); //PROBLEM!
out.write(attachment);
input.close();
out.close();
return path;
} catch (IOException e) {
Log.e("EmailClient", "saveFile: File not saved", e);;
}
}
return path;
}
Upvotes: 4
Views: 27434
Reputation: 1
If you are building your app with target version API level greater than 27 then the storage access disabled by default. If you want to enable it quickly, add the following line in your AndroidManifest.xml
file.
<manifest ... >
<application
android:requestLegacyExternalStorage="true" //add this line
... >
...
</application>
</manifest>
Upvotes: 1
Reputation: 1854
I think you are trying to save the file in the "DIRECTORY_DOWNLOADS" directory,. So you should follow the below code. The below line gives the directory.
path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
If You want to create specific folder "Email_Client" inside that path, write this line.
File folder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS)+"/Email_Client/");
And you should declare the permission "WRITE_EXTERNAL_STORAGE"
String fileName="myfile.txt";
String input="Hello World";
String path = Environment.getExternalStorageState();
File file=null;
if (Environment.MEDIA_MOUNTED.equals(path)) {
try {
byte[] attachment = input.getBytes();
File folder = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS)+"/Email_Client/");
folder.mkdirs();
file=new File(folder,fileName);
//Automatically creates the new empty file specified by the name, if it is not exist.
file.createNewFile();
Log.i("EmailClient", "saveFile: Dir created");
FileOutputStream out = new FileOutputStream(file);
out.write(attachment);
out.close();
} catch (IOException e) {
Log.e("EmailClient", "saveFile: File not saved", e);;
}
}
Please refer this URL: https://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)
Upvotes: 2
Reputation: 2342
use this code
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/EmailClient/");
folder.mkdirs();
File file = new File(folder,filename);
file.createNewFile();
Upvotes: 6