I'm new to Android app development. Currently, I'm trying to get Android download folder path and log it. To do this, I use commons io library. I've got this code:
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
Log.e("files", folder.listFiles().length + " items");
try {
String DFOLDER = FileUtils.readFileToString(folder);
Log.i(TAG2, DFOLDER);
}catch (IOException e){
Toast.makeText(getApplicationContext(), R.string.fail,
Toast.LENGTH_SHORT).show();
}
For some reason I'm getting IOException every time, but I'm receiving the amount of files in folder from 2nd line.
I've got this permissions in manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
FileUtils.readFileToString Reads the contents of a file into a String. Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) return a directory, not a file, so using readFileToString on it will throw an IOException. If you want to read the contents of all the files in the Download directory, do something like that.
File folder = Environment.getExternalStoragePublicDirectory(Environmeng t.DIRECTORY_DOWNLOADS);
for(File file:folder.listFiles()){
try {
String DFOLDER = FileUtils.readFileToString(file);
Log.i("File Contents ", DFOLDER);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Failed!", Toast.LENGTH_SHORT).show();
}
}
If there are too many files in that directory, it will probably throw an OutOfMemoryError.
EDIT: if you need the path of the foler use
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
Log.i("Folder Path",folder.getAbsolutePath());
According to the Commons IO 2.4 API, the readFileToString method will take a file as parameter then read the contents of a file into a String. However,the getExternalStoragePublicDirectory method returns a folder instead of a file so calling readFileToString will throw an IO exception.
Related
Some questions about working with files on android. Is it possible to read files from project? Like from "src" directory. Also, how should I create files?
File file = new File("a.txt");
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
Doesn't work here.
There are two types of storage facility available for file saving
"internal" and "external" storage
To create a file internally
File file = new File(context.getFilesDir(), filename);
getFilesDir() returns a File representing an internal directory.
To learn more please go through https://developer.android.com/training/data-storage/files
Due to some requirement I want to save text files files to Android's file system and read it anytime programmatically.
For each user who will be using the same phone there will be a different text file stored. Unfortunately, at that time when the user has not logged in the only unique information about the user I have is the email address (or is there anything else?).
So my question is can I use the email address as the filename for these .txt files such as "xyz_123#email.com.txt" since the email address can have multiple special characters which I'm not sure are allowed in filenames?
Try this .
1.create
String email = "xyz_123#email.com";
File file = new File(email + ".txt");
if (!file.exists()) {
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
Log.e("FILE_NAME", file.getName());
2.Add permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
3.And you could use https://github.com/permissions-dispatcher/PermissionsDispatcher to request permission .
Now I'm making some version of File Manager in Andoid.
My permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
When I write new file to /mnt/sdcard (External Stroage) everything is allright.
But When i write new file to /mnt/sdcard2 (internal storage) I get IOException like
open failed: EACCES (Permission denied)
My full code:
try {
File existingFile = new File(path);
File newFile = new File(newPath);
if (!newFile.exists()) {
if (!isFolder)
{
newFile.createNewFile();
}
}
if (isFolder) {
FileUtils.copyDirectory(existingFile, newFile);
}
else
{
FileUtils.copyFile(existingFile, newFile);
}
} catch (IOException e) {
result = 1;
}
When i test my application I use real device.
My Path is like '/mnt/sdcard/Music/blabla' and so on
My Path is like '/mnt/sdcard/Music/blabla'
Do not use hardcoded paths as these may (and will) differ depending on device model or can even OS version. You got methods in Environment class to get you root folder of external storage and you should use it like getExternalStorageDirectory()
EDIT
. I need to get folder that contents folder like Music, DCIM, Download, but in Inner Storage of Smartphone
You cannot have "private" DCIM, Downloads really if you want to use system features like DownloadManager or external Camera app, because these apps will simply not be able to write to your private storage. So you either download/take photo yourself - then you can save the file whenever you want, or you use what is it now available, with all the pros and cons.
Name "external"/"internal" is a bit misleading nowadays, so do not take it too literally.
As you can notice from title, I have a problem with writing file to sdcard in Android. I've checked this question but it didn't help me. I want to write file that will be in public space on sdcard so that any other app could read it.
First, I check if sdcard is mounted:
Environment.getExternalStorageState();
Then, I run this code:
File baseDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
baseDir.mkdirs();
File file = new File(baseDir, "file.txt");
try {
FileOutputStream out = new FileOutputStream(file);
out.flush();
out.close();
Log.d("NEWFILE", file.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
I have:
<manifest>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application>
...
</application>
</manifest>
in my AndroidManifest.xml.
Exact error is this:
java.io.FileNotFoundException: /storage/1510-2908/Download/secondFile.txt: open failed: EACCES (Permission denied)
I'm testing my code on emulator (emulating Nexus5 API 23). My minimum required SDK version is 19 (4.4 Kitkat).
Also, everything works fine with writing files to private folder on sdcard with same code so I'd say former code should work too:
File newFile = new File(getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS), "esrxdtcfvzguhbjnk.txt");
newFile.getParentFile().mkdirs();
try {
FileOutputStream out = new FileOutputStream(newFile);
out.flush();
out.close();
Log.d("NEWFILE", newFile.getAbsolutePath());
} catch (IOException e) {
e.printStackTrace();
}
Does anyone have any clue what could be the problem? Could it be that somehow KitKat 4.4 just doesn't allow writing to public space in sdcard anymore or?
Everything works fine with writing files to private folder on sdcard
Beginning with Android 4.4, these permissions are not required if you're
reading or writing only files that are private to your app. For more information, see saving files that are app-private.
FileNotFoundException (permission denied) when trying to write file to sdcard in Android
As you are trying to write the file in emulator having API 23(Marshmallow), You need to Request WRITE_EXTERNAL_STORAGE permission at runtime also. Check this and this for more detail.
I am trying to output numeric values one at a time from an Android application I'm writing, but I'm having a lot of trouble figuring out what's going on. Tried looking for answers, but only confused further. This strikes me as something that should be relatively straightforward, so I feel pretty dumb for being so confused by it.
String directory = Environment.getExternalStorageDirectory().getAbsolutePath();
When I log the directory I get a path "/storage/emulated/0" Where is that? Is that different from what I would get if I wasn't debugging?
Then I have:
String fileName = directory + "/Android/data/com.sample.app/files/test.txt"
File myFile = new File(fileName);
FileOutputStream fos;
try {
fos = new FileOutputStream(myFile);
String text = "Test text\n";
fos.write(text.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e){
e.printStackTrace();
}
I tried using the Windows Explorer to figure out where stuff is saving and/or is supposed to be saved but I don't see it. This is code based on the information in this link: http://developer.android.com/guide/topics/data/data-storage.html, but I really don't understand where the "/storage/emulated/0" comes from and how I either access that location or get rid of it.
EDIT: Right now I just want to save all the numbers so I can check what is coming out. The numbers are recorded from the audio input.
EDIT: Using the ASTRO File app on my phone revealed the files
Didn't need the "/Android/data/com.sample.app/files/" part, don't know how to use that.
Path wrong?
/storage/emulated/0 is a path at your filesystem which represents the external storage. At earlier versions of Android we often had /mnt/sdcard/ or something similiar, but many devices today don't have a sdcard but emulate an external storage anyway.
To view the files at your Android filesystem I'd recommend to use an App like Astro File Manager. Just take a look if your file has been written.
One possible mistake could be, that you you are missing a File.separator between your directory and the local path.
String fileName = directory + File.seperator + "Android/data/com.sample.app/file/test.txt"
Directory created?
You should also make sure, that the directory exists by calling myDir.mkDirs();, where myDir is the complete path without the filename.
To create the directory you can use the following code
directory = directory + "/Android/data/com.sample.app/file/test.txt"
new File(directory).mkDirs();
Uses-Permission in Manifest?
Last error source could be, that you might miss the external storage permission, you need a
You also need to make sure, that you require the permission for writing to the external storage. Take a look for <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> in your Android Manifest file.