欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android4.4下MediaProvider无法向外置SD卡中文件写数据的解决方法

程序员文章站 2023-11-26 16:47:52
本文实例讲述了android4.4下mediaprovider无法向外置sd卡中文件写数据的解决方法。分享给大家供大家参考,具体如下: android4.4平台限制应用对...

本文实例讲述了android4.4下mediaprovider无法向外置sd卡中文件写数据的解决方法。分享给大家供大家参考,具体如下:

android4.4平台限制应用对外置sd卡的读写权限。mediaprovider通过 checkaccess方法 限制对外置sd卡的读写。

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);
   }
  //外置sd卡,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);
  }
}

从以上代码我们看出,如果sexternalpath 没有指向外置sd卡并且path 是外置sd卡的文件路径,那么该方法 就会抛出filenotfoundexception,sexternalpath 一般都是指向内部存储

在应用中 我们通常 通过contentresolver.openoutputstream(uri) 来打开存储卡上媒体文件的文件流,如果媒体文件在外置sd卡上,那么我们就无法打开对应的文件流,自然肯定无法向其中写数据。

为了解决该问题,我们只能改变android4.4平台下mediaprovider 对向sd卡写数据的限制,具体修改方式如下

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);
   }
  //外置sd卡,iswrite = true
  } else if (iswrite) {
   // don't write to non-cache, non-sdcard files.
   throw new filenotfoundexception("can't access " + file);
  } else {
   checkworldreadaccess(path);
  }
},

对于满足issecondaryexternalpath(path) 的文件路径,我们都可以进行读写,对于外置sd卡的文件而言 issecondaryexternalpath(path) 肯定为true

希望本文所述对大家android程序设计有所帮助。