Question

I sent to the emulated android device SD card using DDMS and I can even see these in DDMS. The file names are POS1.png and POS2.png. The sdcard path is /storage/sdcard/foldername

In my code I want to check if my file isFile() so i have written

String filepath = "/storage/sdcard/foldername/POS1.png";
File file = new File(filepath); 

if (!file.isFile()){
//do something
}
else{
//do something else
}

The if condition is always returning true (i.e. file is not a file). I think it may be a permission issue (the current permissions on the files are u=>rwx, g=>rwx) so I am trying to change the permission of the file from adb shell:

  1. I launched my program in the emulator
  2. Opened cmd (on windows. path for adb is already set in environment)
  3. Typed the following codes

C:\> adb shell
root@generic:/ # su
Output: su
root@generic:/ # chmod -R 777 /storage/sdcard/foldername/
Output: chmod -R 777 /storage/sdcard/foldername/
root@generic:/ # ls -l /storage/sdcard/foldername/
The permissions still show the same. Even in the DDMS they're the same. I even restarted the device and eclipse, no change. I tried the following variations too:
root@generic:/ # chmod -R 777 /storage/sdcard/foldername/*
root@generic:/ # chmod -R 777 /storage/sdcard/foldername/*.png
root@generic:/ # chmod 777 /storage/sdcard/foldername/POS1.png
Using u+rwx / g+rwx / o+rwx / a+rwx in place of 777 gives me a "Bad Mode" message

Thank you in advance for the help!

Edit: So I used the following code to check the exception:

try {
    FileInputStream fis = null;
    fis = new FileInputStream("/storage/sdcard/foldername/POS1.png");
} catch (IOException e) {
    Log.e("openfile", "Exception:"+e.getMessage());
}

I got the message open failed: EACCES (Permission Denied)"

Was it helpful?

Solution 2

ChuongPham's answer is correct, if a little dry.

So, to fix this problem you have to add the "Uses" permission to your manifest file (by default this should be "AndroidManifest.xml"), which, in XML, looks just like ChuongPham wrote, and should be inside the "manifest" tag, but not deeper (not in "application" or any other).

From Eclipse:

  1. Double click on AndroidManifest.xml will open a "Android Manifest Permissions" editor
  2. Click the "Add" button in the editor
  3. In the popup window choose "Uses Permission", click "OK"
  4. When the popup closes, in the "Android Manifest Permissions" editor a new item will appear in the "Permissions" list-box on the left (and will be selected). On the right, there is "Attributes for Uses Permission" part, which has "Name" combo-box. Select android.permission.WRITE_EXTERNAL_STORAGE in it.
  5. Save the manifest file and you're done. Next time you start your app, file open will succeed.

OTHER TIPS

You need this permission:

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

Also, have a look at this link for some caveats relating to API 19 and later.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top