21

In our application we are storing pdf files into internal storage.Now i want to get its filepath and need to store into DB. Please tell me how to get its file path. below code:

public void storeIntoInternalMem(byte[] databytes, String name) {
        try
        {
            FileOutputStream fileOuputStream = openFileOutput(name, MODE_PRIVATE);
            fileOuputStream.write(databytes);
            fileOuputStream.close();

        } catch (IOException e)
        {
            e.printStackTrace();
        }

    }
Aarushi
  • 251
  • 1
  • 3
  • 9

4 Answers4

32

you can use Context.getFilesDir() method to get the path to the internal storage

Edit

when using this line FileOutputStream fileOuputStream = openFileOutput(name, MODE_PRIVATE); the internal storage path is obtained by the default implementation of the openFileOutput() method. A new file is created at that location with the specified name. Now when you want to get the path of the same file, you can use the getFilesDir() to get the absolute path to the directory where the file was created. You can do something like this :-

File file = new File(getFilesDir() + "/" + name);

d3m0li5h3r
  • 1,957
  • 17
  • 33
12

String dir = getFilesDir().getAbsolutePath();

VikingGlen
  • 1,705
  • 18
  • 18
0
 getFileStreamPath(String name) of Context.

Returns the absolute path on the filesystem where a file created with

openFileOutput(String, int) 

is stored.

Droid Teahouse
  • 893
  • 8
  • 15
0

On kotlin:

"$filesDir/image.png"

Leonid Ivankin
  • 579
  • 4
  • 9