Android: external storage permission issue:
I really need someone here to guide me about some issues with the external开发者_StackOverflow storage OR sd-card. I won't go into complexities. I have a folder with the name of MyVideos
. It is located in the sd-card folder of Motrola Xoom; the path is "/mnt/sdcard-ext/MyVideos.
The folder is already there. However, there are some strange errors I am having.. For example, if I check if the folder exists or not using the following code:
File myDirectory = new File(defaultStorage, "/MyVideos/");
if (myDirectory.exists())
{
my code: lets say true
}
else
{
my code: false
}
where defaultStorage is = "/mnt/sdcard-ext"
. It always return false. It should return true since the folder surely exists there. And assuming that the folder exists there, I perform other operations like
for (File f : myDirectory.listFiles())
{
if (f.isFile())
{
filenames.add(f.getName()); //add to array
}//if closes
}//for closes
I get FATAL exception at the following line
for (File f : myDirectory.listFiles())
The error stack is below:
E/AndroidRuntime(22644): FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity
E/AndroidRuntime(22644): Caused by: java.lang.NullPointerException
I have just mentioned the things that are important from the stack. It is a NullPointerException to be precise. Although I am using
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in my manifest file. Even then it doesn't work. The most surprising element is that it is running fine here; but there is a client in USA, it is crashing on all his devices.. Is there any difference in the permission settings of devices in UK or in USA.. I shall be thankful to you if you help me here.. It is a big problem for me. Thanks
According to the documentation, the state you're describing means that the external media storage isn't accessible to you:
public static final String MEDIA_SHARED
Added in API level 1 getExternalStorageState() returns MEDIA_SHARED if the media is present not mounted, and shared via USB mass storage.
Constant Value: "shared"
You need to go to your USB Mass Storage options and turn off USB storage.
PS : Thanks to DigCamara I copied from this SO answer.
Thanks
精彩评论