Home » Android » Get the Path from a File inside file directory in Android

Get the Path from a File inside file directory in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a file named abikor.txt inside the files directory in the private app folder:

/data/data/myapp/files/abikor.txt

How can I get the path of this file?

I have seen the following questions but they don’t address my problem.

(In general, how to get the path of a file stored under /files directory in the private app directory?)

Kind Regards

How to&Answers:

For the Internal Storage path you need to specify some thing like this

String path = context.getFilesDir().getAbsolutePath();

Then create a file object from the path

File file = new File(path + "/abikor.txt");

and if you want to Read the file from it then

int length = (int) file.length();

byte[] bytes = new byte[length];

FileInputStream in = new FileInputStream(file);
try {
    in.read(bytes);
} finally {
    in.close();
}

String contents = new String(bytes);