1

I have a text file with some data that I want to copy in an SQLite database. I can not access to files on "downloads" directory of the phone I have an Permission denied exception (I use the path provided by the Device File Explorer of Android-Studio-3.1.4 that allows me to create a file, get its path, edit the content but not saving it!).
Some forums tells that I have to put the file on data/data/app-name but I technically failed to do that.
I tried to create a new file in my project, but still I do not know how to access it with code.

    FileInputStream file = new FileInputStream("/storage/self/primary/Download/file.txt"));

before this line I tried to add:

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

But this is not the solution.
So where should I put the file? what will be the right path? and what are the required permissions that I should add in the manifest? thanks.

user2674471
  • 35
  • 2
  • 8

1 Answers1

0

Before accessing any file from storage you need permission.

For Reading : <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

For Writing : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> (Read permission is explicitly granted for this permission)

Also, you need to request those permission at runtime on device running Android M and Above.

Check following link : https://developer.android.com/training/permissions/requesting

P Vartak
  • 434
  • 5
  • 17