File.canWrite() returns false on some devices although permission provided.
In my application I need to write data to External Sd Card. However this works on some devices but fails on some other . for instance Works : Limebox Internal Flash, SDCard, android OS: 2.3.3 Samsung Sdcard, extSdcard, android OS: 4.0.3
Fails : Leoxys sdcard, extsd android OS:4.0.3
How to find out Android Compatible Devices Dynamically i.e which allow External SD card write operation. myfile.canWrite
Detail: The getExternalSD() is an user defined function which will return the external sdcard path
In my case the external Sdcard path is "/mnt/extSdcard" (or) "/mnt/extsd" I am finding the external sdcard path by reading the file "/system/etc/vold.fstab"
Here is some code Snippet : it creates a folder in the external sdcard. On some devices , myfile.canWrite() returns False and I am unable create folder.
String sdcardpath = getExternalSD();
System.out.println("the sdcard path is:"+sdcardpath.trim().toString());
tv = (TextView) findViewById(R.id.textView1);
tv.append("the sd card path is:"+sdcardpath);
File myfile = new File(sdcardpath);
tv.append("\n the extsd is readable or not:"+myfile.canRead());
tv.append("\n \n the extsd is writable or not:"+myfile.canWrite());
if(myfile.canWrite()){
File testfile = new File(sdcardpath, "test");
if(testfile.exists()){
}else{
boolean mkdir = testtlie.mkdirs();
if(mkdir == true){
tv.append("\n\n the path is:"+sdcardpath.trim().toString()+"/myfile");
}else{
tv.append("\n File does not have permissions");
}
}
}else{
System.out.println("the external sd card does not have write permissions");
tv.append("\n\n the external sd card does not have write permissions");
}
xml file snippets.
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
I would like to get suggestions on
a. Are there any apis that lets the Developer know if the Devices is Truely Android compatible ?
b. Why there are difference in behaviour on different hardware devices. Is this related to firmware ?
c. Are there any alternate ways to achieve write on this kind of hardware ?
I alreday tried modifying permissions of file "platform.xml" under /system/etc/premissions/ using terminal Emulator But I am not able to open the file in write mode
By default android provides Environment.getEnternalStorageDirectory(). But the library returns the "/mnt/sdcard" which will be the internal memory in some devices.
Your inputs will be greatly appreciated !!
File.canWrite() returns false on some devices although permission provided.
That is because there is no permission for what you are trying to do.
In my application i need to write data to External Sd Card
Android applications do not necessarily have access to "External Sd Card". Not all devices even have "External Sd Card". Hence, your "need" is flawed and needs to be replaced.
How to find out Android Compatible Devices Dynamically i.e which allow External SD card write operation. myfile.canWrite
You don't.
In my case the external Sdcard path is "/mnt/extSdcard" (or) "/mnt/extsd"
Those paths may exist on some devices for some firmware versions. Whether there is "External Sd Card" is up to the device manufacturer. Whether it is mounted, and if so, at what point, is up to the device manufacturer. What the permissions are is up to the device manufacturer. And those answers may change at any time, on any device, with a firmware upgrade.
I am finding the external sdcard path by reading the file "/system/etc/vold.fstab"
That is incrementally better than random guessing, but you are going well beyond the scope of the Android SDK.
On some devices , myfile.canWrite() returns False and I am unable to create folder.
Of course.
By default android provides Environment.getEnternalStorageDirectory().
This returns a File
pointing to external storage -- what the user will have access to if they attach a USB cable between the device and a host computer and mount external storage as a drive or volume.
a. Are there any apis that lets the Developer know if the Devices is Truely Android compatible ?
What you are trying to do is not "Truely Android compatible". The devices are fine -- your expectations are not. Please fix your expectations.
Why there are difference in behaviour on different hardware devices.
Anything that is outside the scope of the Android SDK is up to device manufacturers. They can do what they want.
Are there any alternate ways to achieve write on this kind of hardware ?
Nothing that will be reliable. Just use official external storage (e.g., getExternalFilesDir()). Or, build your own ROM mod that you control, so you dictate the terms of what non-standard mount points exist and what their read/write permissions are, and use that ROM mod on whatever devices you wish to and can.
to your question b: because (unfortunately) there's still no real standardization on Android, APIs may vary by manufacturer and you can often face device-specific problems. A good example is the getData() method which returns null on Samsung 2.3.x devices.
Environment.getEnternalStorageDirectory()
- I suppose this will return internal memory directory only on devices which do not have an sd-card so I don't see a problem there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With