Android4.4 MediaProvider cannot write data to files in an external SD card

  • 2020-09-28 09:09:27
  • OfStack

This article illustrates the solution to MediaProvider's inability to write data to files in an external SD card under Android4.4. To share for your reference, the details are as follows:

The Android4.4 platform restricts read and write access to an external SD card. MediaProvider restricts reading and writing to external SD CARDS through the checkAccess method.


private void checkAccess(Uri uri, File file, int modeBits) throws FileNotFoundException {
  final boolean isWrite = (modeBits & 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)
    == PackageManager.PERMISSION_GRANTED);
  if (path.startsWith(sExternalPath) || path.startsWith(sLegacyPath)) {
   if (!readGranted) {
    c.enforceCallingOrSelfPermission(
      READ_EXTERNAL_STORAGE, "External path: " + path);
   }
   if (isWrite) {
    if (c.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
      != PackageManager.PERMISSION_GRANTED) {
     c.enforceCallingOrSelfPermission(
       WRITE_EXTERNAL_STORAGE, "External path: " + path);
    }
   }
  } else if (path.startsWith(sCachePath)) {
   if (!readGranted) {
    c.enforceCallingOrSelfPermission(
      ACCESS_CACHE_FILESYSTEM, "Cache path: " + path);
   }
  // The outer SD The 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 this method will throw FileNotFoundException. sExternalPath 1 generally points to internal storage

In the application, we usually use contentresolver.openOutputStream (uri) to open the file stream of the media file on the memory card. If the media file is outside the SD card, then we cannot open the corresponding file stream and certainly cannot write data to it.

In order to solve this problem, we can only change the restriction of Mediaprovider on Android4.4 platform to write data to SD card. The specific modification is as follows


private void checkAccess(Uri uri, File file, int modeBits) throws FileNotFoundException {
  final boolean isWrite = (modeBits & 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)
    == PackageManager.PERMISSION_GRANTED);
  if (path.startsWith(sExternalPath) || path.startsWith(sLegacyPath) || isSecondaryExternalPath(path)) {
   if (!readGranted) {
    c.enforceCallingOrSelfPermission(
      READ_EXTERNAL_STORAGE, "External path: " + path);
   }
   if (isWrite) {
    if (c.checkCallingOrSelfUriPermission(uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
      != PackageManager.PERMISSION_GRANTED) {
     c.enforceCallingOrSelfPermission(
       WRITE_EXTERNAL_STORAGE, "External path: " + path);
    }
   }
  } else if (path.startsWith(sCachePath)) {
   if (!readGranted) {
    c.enforceCallingOrSelfPermission(
      ACCESS_CACHE_FILESYSTEM, "Cache path: " + path);
   }
  // The outer SD The 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);
  }
},

For file paths that satisfy isSecondaryExternalPath(path), we can read and write, and isSecondaryExternalPath(path) must be true for files with external SD CARDS

I hope this article has been helpful in Android programming.


Related articles: