Android Development EXTSDCARD naming on different devices? - android

I have a variable that just states outright:
File file = new File("/Extsdcard")
Am I foolish enough to think all android devices mount an external devices in this wording?
or should I be using Environment.getExternalStorageDirectory().getAbsolutePath();
My application searches through the first three folder layers so this is quite important to the functionality.

String path = Environment.getExternalStorageDirectory().toString()+"/YourDirectory";
Log.d("Files", "enter code herePath: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
//It gives you list of all directoris/files list
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < 3; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}

Environment.getExternalStorageDirectory() gets the top level of the external drive.

Related

Java listFiles return null but I can still find files when I have a full path to a file in the directory

So in my react-native module I Want to find the already created files that my app has created in
/storage/emulated/0/Download/xx/xx
It work great when My app create those files in those directory and File.listFiles("/storage/emulated/0/Download/xx/xx") return all those files without any problem.
Now when I uninstall my app and install it again, listFiles cant find those old files anymore!!
But only find the files that I create a new.
Now for those of you who think this is about permission I already check it and have full access
status{"android.permission.WRITE_EXTERNAL_STORAGE":"granted","android.permission.READ_EXTERNAL_STORAGE":"granted"}
Here is the Java method that I have
#ReactMethod(isBlockingSynchronousMethod = true)
public String getAllAppDownloadedFiles(String folderPath) {
File folder = new File(folderPath);
String result = "";
if (!folder.exists())
return result;
File[] files = folder.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files[i];
if (file.isFile())
result += file.getAbsolutePath() + (files.length - 1 > i ? "," : "");
}
return result;
}
I should point out that the problem only exist when I run the app on my mobile and not in emulator. I suspect it has to do with the version of API.
Any Idea how I could fix this?

How can I list files in a directory in Android?

I have a folder in root with some files I want to list but the array returns null:
String path = getenv("EXTERNAL_STORAGE") + "/Files";
File directory = new File(path);
File[] files = directory.listFiles();
Permissions are granted
First off, you need to use Environment.getExternalStorageDirectory() instead of getenv():
String path = Environment.getExternalStorageDirectory().toString() + "/Files";
File directory = new File(path);
File[] files = directory.listFiles();
for (int i = 0; i < files.length; i++)
Log.d("Files", "FileName:" + files[i].getName());
But a very important note is the /storage/emulated/0 folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored. That's why your array returns null. You'll need to find the actual physical location on your device where it is stored.

Android: can write but can't read file from Environment.DIRECTORY_PICTURES

I'm trying to save pictures on my Android phone and then restore them when it's needed.
So I use RxJava to save and read this files to Environment.DITECTORY_PICTURES folder. My problem is that I can write an image (my rx method calls onNext, but not onError, debug shows the correct path, settings testify that app data size grows), but can't read from there.
When I use my reading code with the correct path its throws FileNotFoundException. When I use this test code
try {
String path = Environment.DIRECTORY_PICTURES;
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
} catch (Exception e) {
e.printStackTrace();
}
it throws NPE saying that files array is null.
I have Read/Write external storage permissions granted. Tested on Android 5 and 6, so I guess there is nothing about Runtime permissions as well.
Is there something special about reading from Environment folders I don't know?
Thanks in advance for your help!
Environment.DIRECTORY_PICTURES is not a fulll qualified path to the file system directory, the one you looking for is get trough the class and using this as argument.
File directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
Theres more kind of storages described at:
https://developer.android.com/reference/android/os/Environment.html

get the list of files and folders exist in root directory

in my program I need something like file manager to list the files and directories of internal and external memories . for example see the picture below...
Example of what I want
how can I get the root directory
in fact I want to get a list like picture above that include internal and external directory .
thanks
Getting the list of files and folders exist in root directory, like this:
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}

List all the files from a specific folder in android

I tried this one : How to access all mp3 files from all the subfolders in the sdcard?
I need to list all .amr files from a folder (ABC) and on click of any list item, do something.
Thanks in advance.
You can try this:
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File f = new File(path);
File file[] = f.listFiles();
Log.d("Files", "Size: "+ file.length);
for (int i=0; i < file.length; i++)
{
//here populate your listview
Log.d("Files", "FileName:" + file[i].getName());
}
This will list all the files in the pictures folder. Use the path to your folder to get the files in that folder
Dont forget to use the following permission
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Categories

Resources