The example of this article describes the solution to the mediaprovider that cannot write data to an external SD card under Android4.4. Share to everyone for your reference, specific as follows:
The Android4.4 platform restricts the use of read and write access to external SD cards. Mediaprovider through the CheckAccess method restricts the reading and writing of the external SD card.
private void CheckAccess (URI uri, file file, int modebits) throws FileNotFoundException {Final Boolean iswrite = (mode
Bits & Mode_write_only)!= 0;
Final String path;
try {path = File.getcanonicalpath ();
catch (IOException e) {throw new IllegalArgumentException ("Unable to resolve canonical path for" + file, e);
Context C = GetContext (); Boolean readgranted = (C.checkcallingorselfuripermission (uri, intent.flag_grant_read_uri_permission) = = PackageMan Ager.
permission_granted); if (Path.startswith (sexternalpath) | | | path.startswith (SLEGACYPATH)) {if (!readgranted) {C.enforcecallingorselfper
Mission (read_external_storage, "EXTERNAL path:" + path); } if (Iswrite) {if (C.checkcallingorselfuripermission (URI, intent.flag_grant_write_uri_permission)!= Packag emanager.permission_granted) {c.enforcecallingorselfpermission (write_external_storage, "EXTERNAL path:" + P
ATH); }} else if (Path.startswitH (Scachepath)) {if (!readgranted) {c.enforcecallingorselfpermission (Access_cache_filesystem, "CACHE path:
"+ path);
}//external SD card, Iswrite = true} else if (iswrite) {//don ' t write to Non-cache, non-sdcard files.
throw new FileNotFoundException ("Can ' t access" + file);
else if (Issecondaryexternalpath (path)) {//Read access is OK with the appropriate permission if (!readgranted) {
C.enforcecallingorselfpermission (read_external_storage, "EXTERNAL path:" + path);
} else {checkworldreadaccess (path);
}
}
As we can see from the above code, if Sexternalpath does not point to an external SD card and path is the file path of an external SD card, then the method throws a filenotfoundexception,sexternalpath that typically points to internal storage
In the application, we usually open the file stream of the media file on the memory card by Contentresolver.openoutputstream (URI), if the media file is on the external SD card, then we can't open the corresponding file stream, and naturally we can't write the data to it.
In order to solve this problem, we can only change the Android4.4 platform under the Mediaprovider to the SD card to write data restrictions, the specific way to modify the following
private void CheckAccess (URI uri, file file, int modebits) throws FileNotFoundException {Final Boolean iswrite = (mode
Bits & Mode_write_only)!= 0;
Final String path;
try {path = File.getcanonicalpath ();
catch (IOException e) {throw new IllegalArgumentException ("Unable to resolve canonical path for" + file, e);
Context C = GetContext (); Boolean readgranted = (C.checkcallingorselfuripermission (uri, intent.flag_grant_read_uri_permission) = = PackageMan Ager.
permission_granted); if (Path.startswith (sexternalpath) | | | path.startswith (slegacypath) | | issecondaryexternalpath (PATH)) {if (!readGrante
d) {c.enforcecallingorselfpermission (read_external_storage, "EXTERNAL path:" + path); } if (Iswrite) {if (C.checkcallingorselfuripermission (URI, intent.flag_grant_write_uri_permission)!= Packag emanager.permission_granted) {c.enforcecallingorselfpermission (write_external_storage, "EXTERNAL path:" + P
ATH); }
} else if (Path.startswith (Scachepath)) {if (!readgranted) {c.enforcecallingorselfpermission (ACCES
S_cache_filesystem, "CACHE path:" + path);
}//external SD card, Iswrite = true} else if (iswrite) {//don ' t write to Non-cache, non-sdcard files.
throw new FileNotFoundException ("Can ' t access" + file);
else {checkworldreadaccess (path); }
},
We can read and write to a file path that satisfies Issecondaryexternalpath (path), and Issecondaryexternalpath (path) must be true for external SD card files
I hope this article will help you with the Android program.