1

I am using Android Studio and a virtual device (Nexus 5 S on x86).

I am trying for tests purposes to read a binary file from external storage.

When I connect to the device with 'adb shell' to the device, I can find the file in /storage/emulated/0

In my test activity, I just put this:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final String filename = "basse-normandie.map";

    final File file = new File(Environment.getExternalStorageDirectory()
                      .getAbsolutePath(), filename);
    Log.i("FileTests", "file is '"+file.getAbsolutePath()+"'");
    Log.i("FileTests", "file exists: "+file.exists());
    file.setReadable(true);
    Log.i("FileTests", "app can read file: "+file.canRead());

}

and in the manifest I put

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

I guess WRITE_EXTERNAL_STORAGE is not useful though.

In the logcat window, I get this:

06-25 19:03:46.648 5107-5107/org.airforce_one.filetests I/FileTests: file is '/storage/emulated/0/basse-normandie.map'
    file exists: true
    app can read file: false

and I can't find what I'm doing wrong... Every page I've looked on the net tells me to give the permissions, but no other hints...

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user3605616
  • 79
  • 1
  • 5

2 Answers2

1

Remove final in the line

 File file = new File(Environment.getExternalStorageDirectory()
                  .getAbsolutePath(), filename);

And change

file.setReadable(true, false); //the second parameter is specify if the permission is for OwnerOnly
SafalFrom2050
  • 687
  • 6
  • 12
  • You need to set readable permission like "file.setReadable(true,false)" because your above method sets readable permission for OwnerOnly. – SafalFrom2050 Jun 25 '18 at 18:54
  • Thanks for this precision, I didn't know about it. However, even with this modification, 'file.setReadable(true,false)' gives a value of false, so my app doesn't have the permission to set the file to readable-by-all, and the subsequent 'file.canRead()' still gives false as an answer... – user3605616 Jun 25 '18 at 19:24
0

I had the exact same issue with a .map file with

file.setReadable(true, false);

solved it.

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Kann Nix
  • 13
  • 2