android7.0 cdd文档,android7.0文件共享

适配7.0 文件共享方式

1、首先再res目录下创建xml文件夹 并且在xml文件夹创建fileFrovider的path文件,文件名自定义随意取

文件内容如下,属性解释:

paths :路径根节点 所有的path都写在里面

子节点:五项分别对应代码中为

代表 context.getFileDir()

代表 context.getCacheDir()

代表 Environment.getExternalStorageDirectory()

代表 context.getExternalFilesDir()

代表 getExternalCacheDir()

name:名字随意取,获取uri是会被转换成cotent的uri 把这里的name值作为uri的一部分,我们用不到,所以不需要太多关注,主要是path属性

path:是文件的路径 .是根路径 字符串则是代表文件夹 譬如:path=“aaa” 那么就代表的是该路径下的aaa文件夹

name="fileDir"

path="."/>

name="filePath"

path="."

/>

name="sdcard_root_externalStorageDirectory"

path="updateFile"/>

对应的java代码

public class StorageUtils {

/*

* context.getCacheDir()和context.getExternalCacheDir()

* 目录的路径不同。

* 前者的目录存在外部SD卡上的。在手机里可以直接看到

* 后者的目录存在app的内部存储上,需要root以后,用Root Explorer 文件管理器才能看到

*/

/**

*

* name="fileDir"

* path="."/>

*/

public static File getFileDir(Context context) {

File appCacheDir = context.getFilesDir();

if (appCacheDir == null) {

Log.w("StorageUtils", "Can't define system cache directory! The app should be re-installed.");

}

return appCacheDir;

}

/**

*

* name="filePath"

* path="."

* />

*/

public static File getExternalFilesDir(Context context) {

File appCacheDir = context.getExternalFilesDir(null);

if (appCacheDir == null) {

Log.w("StorageUtils", "Can't define system cache directory! The app should be re-installed.");

}

return appCacheDir;

}

/**

*

* name="cache-path"

* path="." />

* 获取应用的缓存目录

* 路径需要root以后,用Root Explorer 文件管理器才能看到

*/

public static File getCacheDirectory(Context context) {

File appCacheDir = context.getCacheDir();

if (appCacheDir == null) {

Log.w("StorageUtils", "Can't define system cache directory! The app should be re-installed.");

}

return appCacheDir;

}

/**

*

* name="external-cache-path"

* path="." />

* 获取应用的缓存目录 路径在手机里可以直接看到

* apk下载路径为:SDCard/Android/data/com.winfo.update/cache/

*/

public static File getExternalCacheDirectory(Context context) {

File appCacheDir = context.getExternalCacheDir();

if (appCacheDir == null) {

Log.w("StorageUtils", "Can't define system cache directory! The app should be re-installed.");

}

return appCacheDir;

}

/**

*

* name="external-cache-path"

* path="." />

* 在cache下新增自定义缓存路径

* apk下载路径为:SDCard/Android/data/com.winfo.update/cache/update/

*/

public static File getExternalCacheCustomDirectory(Context context) {

//在SDCard/Android/data/com.winfo.update/cache/update创建文件夹

File appCacheDir = new File(context.getExternalCacheDir(), "update");

//如果不存在就创建

if (!appCacheDir.exists()) {

if (appCacheDir.mkdirs()) {//创建成功就返回SDCard/Android/data/com.winfo.update/cache/update/

return appCacheDir;

} else {

//创建失败就返回默认的SDCard/Android/data/com.winfo.update/cache/

return context.getExternalCacheDir();

}

} else {

//存在直接返回

return appCacheDir;

}

}

/**

*

* name="sdcard_root_externalStorageDirectory"

* path="updateFile"/>

* 在cache下新增自定义缓存路径

* apk下载路径为:/storage/emulated/0/updateFile/weixin667android1320.apk

*/

public static File getExternalCustomDirectory() {

//在SDCard/Android/data/com.winfo.update/cache/update创建文件夹

File dir = new File(Environment.getExternalStorageDirectory(), "updateFile");

//如果不存在就创建

if (!dir.exists()) {

if (dir.mkdirs()) {

return dir;

} else {

//创建失败就返回默认的SDCard/Android/data/com.winfo.update/cache/

return Environment.getExternalStorageDirectory();

}

} else {

//存在直接返回

return dir;

}

}

}

示例:安装APK

/**

* 获取安装文件意图

* @param context context

* @param apkFile 安装文件

* @return 安装意图

*/

private static Intent getApkInStallIntent(Context context, File apkFile) {

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N) {

Uri uri = FileProvider.getUriForFile(context, "com.winfo.update.provider", apkFile);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

intent.setDataAndType(uri, "application/vnd.android.package-archive");

} else {

Uri uri = getApkUri(apkFile);

intent.setDataAndType(uri, "application/vnd.android.package-archive");

}

return intent;

}

manifest注册

android:name="android.support.v4.content.FileProvider"

android:authorities="com.winfo.update.provider" 自定义名称会作为uri的一部分

android:exported="false"

android:grantUriPermissions="true">

android:name="android.support.FILE_PROVIDER_PATHS"

android:resource="@xml/update_apk_paths" /> xml文件夹下的paths文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值