欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

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

时间:2021-01-21 10:51:42|栏目:Android代码|点击:

本文实例讲述了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程序设计有所帮助。

上一篇:Android 仿微信聊天时间格式化显示功能

栏    目:Android代码

下一篇:Android 简单封装获取验证码倒计时功能

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

本文地址:http://www.codeinn.net/misctech/48634.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有