Android4.4下MediaProvider无法向外置SD卡中文件写数据的解决方法
本文实例讲述了Android4.4下MediaProvider无法向外置SD卡中文件写数据的解决方法。分享给大家供大家参考,具体如下:
Android4.4平台限制应用对外置SD卡的读写权限。MediaProvider通过checkAccess方法限制对外置SD卡的读写。
privatevoidcheckAccess(Uriuri,Filefile,intmodeBits)throwsFileNotFoundException{
finalbooleanisWrite=(modeBits&MODE_WRITE_ONLY)!=0;
finalStringpath;
try{
path=file.getCanonicalPath();
}catch(IOExceptione){
thrownewIllegalArgumentException("Unabletoresolvecanonicalpathfor"+file,e);
}
Contextc=getContext();
booleanreadGranted=
(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,"Externalpath:"+path);
}
if(isWrite){
if(c.checkCallingOrSelfUriPermission(uri,Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
!=PackageManager.PERMISSION_GRANTED){
c.enforceCallingOrSelfPermission(
WRITE_EXTERNAL_STORAGE,"Externalpath:"+path);
}
}
}elseif(path.startsWith(sCachePath)){
if(!readGranted){
c.enforceCallingOrSelfPermission(
ACCESS_CACHE_FILESYSTEM,"Cachepath:"+path);
}
//外置SD卡,isWrite=true
}elseif(isWrite){
//don'twritetonon-cache,non-sdcardfiles.
thrownewFileNotFoundException("Can'taccess"+file);
}elseif(isSecondaryExternalPath(path)){
//readaccessisOKwiththeappropriatepermission
if(!readGranted){
c.enforceCallingOrSelfPermission(
READ_EXTERNAL_STORAGE,"Externalpath:"+path);
}
}else{
checkWorldReadAccess(path);
}
}
从以上代码我们看出,如果sExternalPath没有指向外置SD卡并且path是外置SD卡的文件路径,那么该方法就会抛出FileNotFoundException,sExternalPath一般都是指向内部存储
在应用中我们通常通过contentresolver.openOutputStream(uri)来打开存储卡上媒体文件的文件流,如果媒体文件在外置SD卡上,那么我们就无法打开对应的文件流,自然肯定无法向其中写数据。
为了解决该问题,我们只能改变Android4.4平台下Mediaprovider对向SD卡写数据的限制,具体修改方式如下
privatevoidcheckAccess(Uriuri,Filefile,intmodeBits)throwsFileNotFoundException{
finalbooleanisWrite=(modeBits&MODE_WRITE_ONLY)!=0;
finalStringpath;
try{
path=file.getCanonicalPath();
}catch(IOExceptione){
thrownewIllegalArgumentException("Unabletoresolvecanonicalpathfor"+file,e);
}
Contextc=getContext();
booleanreadGranted=
(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,"Externalpath:"+path);
}
if(isWrite){
if(c.checkCallingOrSelfUriPermission(uri,Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
!=PackageManager.PERMISSION_GRANTED){
c.enforceCallingOrSelfPermission(
WRITE_EXTERNAL_STORAGE,"Externalpath:"+path);
}
}
}elseif(path.startsWith(sCachePath)){
if(!readGranted){
c.enforceCallingOrSelfPermission(
ACCESS_CACHE_FILESYSTEM,"Cachepath:"+path);
}
//外置SD卡,isWrite=true
}elseif(isWrite){
//don'twritetonon-cache,non-sdcardfiles.
thrownewFileNotFoundException("Can'taccess"+file);
}else{
checkWorldReadAccess(path);
}
},
对于满足isSecondaryExternalPath(path)的文件路径,我们都可以进行读写,对于外置SD卡的文件而言isSecondaryExternalPath(path)肯定为true
希望本文所述对大家Android程序设计有所帮助。