Cannot reach files inside the root directory of my Android app - java

I have the code bellow:
String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/data/com.example.stavr.mydiary";
File file=new File(path);
File[] files = file.listFiles();
String arr[]=file.list();
List l=new ArrayList<>();
for(String i:arr){
if(i.endsWith(". txt")){
l.add(i);
}
}
which returns 0 files on my array (NullPointerException to be exact), Iknow that there are files in that directory. If i change the directory to just
Environment.getExternalStorageDirectory().getAbsolutePath()
the code work and it returns all the files. I thing that i have done something wrong with the permissions. Could you please help me,
Thank you very much in advance..!!!

It seems you're trying to access your internal app data folder, you can do that by using a context object:
String dir = context.getFilesDir().getAbsolutePath();
If you still want to access external storage then you should make sureyou have WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml

Related

/storage/emulated/0/Notes/ (No such file or directory)?

I tried this link:
How to create text file and insert data to that file on Android
But, it says "No such file or directory". Can anybody help me please? Thanks in advance!
---- Updated answer----
I think you can't create a directory inside the internal storage of the device. Except you've a root access for the app. You can only create the directory inside your app private folder within the following path String path = getFilesDir().
you can use like this below -
File mydir = context.getDir("mydirectory", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myAwesomeFile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.
getDir(StringName, int mode) method to create or access directories in internal storage.
/storage/emulated/0/Notes/ will always return No such file or directory except device is rooted and dir.mkDirs() will always return false for this path.
Hope this will help you.

Android File.listFiles() returns null

I am trying to access my android files through my apk and (initially) list them on my terminal. This is the code I use:
File basePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)getAbsolutePath();
File directory = new File(basePath);
System.out.println("" + directory.toString());
File[] directoryContents = directory.listFiles();
I do succeed printing this path: /storage/emulated/0/Download which actually exists in my android and has some files in it.
When listing files inside that directory using:
for (File file : directoryContents)
{
if (file.isFile())
System.out.println("" + file.getName());
else
continue;
}
I get java.lang.NullPointerException: Attempt to get length of null array
I also included <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> in android's manifest.
My smartphone runs on Android 7.0 if that's of any help.
listFiles returns an array if the File instance from which this method is called from is a directory. Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) always(i guess) returns a directory( unless you altered the os defaults). so the only thing going wrong is probably lack of permissions;
here is how to ask permissions properly
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS replace this line instance of Environment.getExternalStorageDirectory()getAbsolutePath() issue will resolved.

Trying to get a file path to a public folder on android device and get its content

I am looking to get the file path of a folder and then get its content. The folders are a public folder that you can access by just connecting the device into the computer. How do I go about getting a the file path to get access to these public folders and then get all its content?
So once I connect my computer to the device these are the folders I see and there is one that says "Folder to get items from" and I can't seem to figure what the file path is in order to get access to its content .
This is how I'm trying to get its content and Log them
File fileOfEpubs = new File("/data/data/Folder of to get items from/");
File[] dirEpub = fileOfEpubs.listFiles();
if (dirEpub.length != 0){
for (int i = 0; i < dirEpub.length; i++){
String fileName = dirEpub[i].toString();
Log.i("File", "File name = " + fileName);
}
}
You appear to be looking at the root of external storage. Programmatically, you access that via Environment.getExternalStorageDirectory(). That requires either the READ_EXTERNAL_STORAGE or WRITE_EXTERNAL_STORAGE permission.
You may need to escape the spaces in your string
File fileOfEpubs = new File("/data/data/Folder\ of\ to\ get\ items\ from/");

Adding files to a file[] statement in java

Hello members of stackoverflow
I need some help with adding files from a directory to a bunch of files (File[])
So basically i want to add all the files in a directory to the following group of files:
File[] contents = {};
The user of my application will select a directory and i want that directories contents to be added to the above group of files ('contents I'm not sure how this is done because it doesn't have a simple 'add' method like an ArrayList/List does.
Any help with this would be greatly appreciated.
Thanks.
Use File.listFiles():
File dir = new File("/somedir");
File[] files = dir.listFiles();
try
{
File folder = new File(FOLDER_NAME);
File[] contents = folder.listFiles();
}
catch(Exception e)
{
System.out.println("[Error] Folder not Found");
}

How to retrieve every file in the Internal Storage?

In Android, assuming that I have files in "/data/data/package.name/", without knowing the names or how many files exist, what is the best way to retrieve/iterate them all?
Also, the name is numerical only.
It looks like you want to list all of the files in the directory, and (possibly) recurse if a file is a directory.
You can find how to do this at the answer to this question.
Copy-pasted:
File f = new File("/data/data/package.name/");
File[] files = f.listFiles();
for (File inFile : files) {
if (inFile.isDirectory()) {
// is directory; recurse?
} else {
doLogicFor(inFile);
}
}

Categories

Resources