This is my first Android app and my first attempt at writing something to file. I'm trying to capture a log according to these instructions and I'm getting the FileNotFoundExeption ENOENT (No such file or directory). That's fair enough because the directory does not exist. But then how do I create the directory? Or use another one? I don't know best practices for where to write logs to email them, nor do I know how to make a new directory for them.
This is the path I'm trying to use.
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
String fullName = path + "mylog";
File file = new File (fullName);
The parent dir doesn't exist yet, you must create the parent first before creating the file
String path = Environment.getExternalStorageDirectory() + "/" + "MyFirstApp/";
// Create the parent path
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
String fullName = path + "mylog";
File file = new File (fullName);
Edit:
Thanks to Jonathans answer, this code sample is more correct. It uses the exists() method.
You also need the permission in your manifest:
<manifest ...>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
...
</manifest>
I'd like to add to Francesco's answer, that instead of asking if it's a directory, you could ask if it exists with dir.exists() method.
And also check that you've set the proper permissions in the Manifest file.
Hope it helps
Jonatan
Related
i know this question might have a simple response, but i can0t find anything that fits my case in the docs.
What i'm trying to achieve is to store a file (E.G test.txt) in /download directory or any other dir that is public and visible by any resource finder.
I don't find any hint of the correct approach in the docs or even if there is the possibility to do it anymore.
Thank you a lot
Try this
val dir = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS))
if (!dir.exists()) {
dir.mkdirs()
}
And don't forget to set this permission in your manifest.xml:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
or you can refer this link also [https://stackoverflow.com/a/28183933/15529296]
To create a file in a custom directory inside the public storage with Java:
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/YOUR_FOLDER_NAME_HERE";
File dir = new File (path);
dir.mkdirs();
path = path + File.separator + "YOUR_FILE_NAME";
File file = new File(path);
After that do whatever you want with your file.
As Yash said don't forget the permission...
I'm new at this.
Telegram, Whatsapp, Vk, Aliexpress - All this apps can write they folders in /Storage/emulated/0
How can I create my folder in this place for Android 5...10?
When I try to use File("/storage/emulated/0/", myfile) - it doesn't work.
Please, can anyone give some mini example how can I create my files in storage
Since you haven't written any code, I can't tell you what you're doing wrong. But this is something that might help you.
First you need to declare permissions on your manifest.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
After that you can simply use this to create your folder.
String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" +
"folderName";
File directory = new File(path);
directory.mkdirs(); // It returns a boolean you can use it to check if your folder
// was created.
Yes, You can create Folders inside the External Storage too.
But, keep in mind that you cannot use emulated/0 because it is different for different devices.
File path = new
File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Folder/");
if(!path.exists()){
boolean k = path.mkdirs();
}
Although, Environment.getExternalStorageDirectory() is deprecated as of API29. So You can use getExternalDir() too.
Could you please help me. renameTo() leaves empty old file. So I see 2 files in file system with new name and old name. The size of old file is 0. If I delete old file after renaming it says that file does not exist while staying in file system.
An absolute path of directory is:
/storage/sdcard0/DCIM/Camera
My code:
String dir = oldpath.substring(0, oldpath.lastIndexOf("/"));
File directory = new File(dir);
File from = new File(directory, oldfilename);
File to = new File(directory, newname);
renamed = from.renameTo(to);
Try this code:
File sdcard = new File("/storage/sdcard0/DCIM/Camera");
File from = new File(sdcard, "from.txt"); // Don't forget to set the file extension.
File to = new File(sdcard, "to.txt"); // In this case, we have a '.txt' file extension.
from.renameTo(to);
You can get the sdcard directory in String type programmatically by using this code:
String sdcard = Environment.getExternalStorageDirectory().getPath();
Don't forget to add this permission in manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
RenameTo leaves an empty copy of the original file if the file is opened by another process.
For example, I wanted to rename a file after the DownloadManager finished downloading it. The DownloadManager apparently notifies the BroadcastReceivers after the download, but before closing the file. This caused the renameTo in the onReceive to leave an empty copy. To solve this problem, I had to make the BroadcastReceiver to wait half a second before renaming the file.
You must remember about two things : The file and the file extension (file type). The following is wrong way sometimes, in case of deleting and renaming file :
File file = new File (dir+"/"+myfile)
The right way is :
File file = new File (dir, myfile+".db");
For the Full Aprroach, you could look at Answer Here.
I'm developping an application where I need to create new folders/files in the sdcard. The thing is I can see them using a root explorer but not with the default one which comes with Android.
I've taken a look at several similar questions here but don't seem to work for me.
For sure, I'm using in my manifest this:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and I'm writing all the Files using:
String path = Environment.getExternalStorageDirectory() + "/FolderName/FileName.format"
but as I said, these new folders/files remained hidden and can only be seen using a root explorer. Neither the folder nor file name starts with "."
Thanks in advance.
Try using mkDirs()
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
I have a routed device and when I do this
adb shell cat /data/misc/bluetooth/dynamic_auto_pairing.conf
it prints the content of this file.
But in my code when I write something like this, it says that the file does not exist. Well from the console I see it I know is there, but from code I can't read it. My question is what is the problem , am I missing some permission or what is the problem ? can someone provide me with some code to read the content from this file.
Thanks
File pa = new File("/data/misc/bluetooth/","dynamic_auto_pairing.conf");
//this doesn't works also
//File pa = new File("/data/misc/bluetooth","dynamic_auto_pairing.conf");
//File pa = new File("/data/misc/bluetooth/dynamic_auto_pairing.conf");
if(pa.exists()){
Log.v("tag", "does exists");
}else{
Log.v("tag", "does NOT exist");
}
If the file is on sdcard, try:
File pa = new File(Environment.getExternalStorageDirectory() + "/data/misc/bluetooth/dynamic_auto_pairing.conf");
Also try to add:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
outside <application></application> in your manifest file.
EDIT
If the file is in internal memory: Your app can read only from a special folder in internal memory. The path to that folder is returned by:
getFilesDir().getAbsolutePath()
So put the file there and read it with openFileInput().
More info:
http://developer.android.com/guide/topics/data/data-storage.html#filesInternal
From the docs for File...
public File (String dirPath, String name)
Constructs a new File using the specified directory path and file name, placing a path separator between the two.
In your code you are using...
File pa = new File("/data/misc/bluetooth/","dynamic_auto_pairing.conf");
...and because your dirPath ends with a separator "/data/misc/bluetooth/" it will result in two separators. In other words, effective path will be...
/data/misc/bluetooth//dynamic_auto_pairing.conf
Note the // after 'bluetooth`
If you are using android 6.0 or higher. You must request permission in code.