getExternalFilesdir Fail
OnActivityCreated I'm doing:
activity.getExternalFilesDir(Environment.DIRECTORY_PICTURES);
In logcat I get:
com.package W/ContextImpl﹕ Failed to ensure directory: /storage/sdcard1/Android/data/com.package/files/Pictures
This happens only on LOLLIPOP (MOTO G 2014), and everything is fine on KITKAT (Nexus 4). I've WRITE_EXTERNAL_STORAGE on manifest. What am I missing here? The storage is mounted and reachable via a file browsing app (like ES Explorer).
EDIT: Surprisingly the file is correctly created under the directory even if I get the warning reported above.
Try this :
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
and use this permission:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Actually:
getExternalFilesDir()
It returns the path to files folder inside /Android/data/com.package/files/Pictures
on your SD card. And there is no folder named Pictures
inside it. So you are getting that error.
I was facing similar issue on only one device. The android version was 5.1.1(api 22) What I found in manifest the permission was added as follows.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
I removed the maxSdkVersion limit and then the problem was resolved.
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I added the maxSdkVersion before because somewhere I read like this permission was required for APIs 18 or older but seems like that is not the case. It differs device by device.
If you have same permission settings in your manifest this might help you.
If you are developing for Android 2.1 or after, use
getExternalFilesDir(String type)
Read carefully https://developer.android.com/guide/topics/data/data-storage.html#filesExternal
Also, you'll need to use this method or something similar
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
then check if you can access the sdcard. As said, read the official documentation.
You must get to directory (a File object) from SD Card and Make your specific File path such as:
Environment.getExternalStorageDirectory(Environment.DIRECTORY_PICTURES);
Also need to give permission in manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
链接地址: http://www.djcxy.com/p/28016.html