Android4.4 Mediaprovider cannot write data to external SD card files _android

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.