1.配置文件AndroidManifest.xml 添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
android 系统版本android6.0以上需要主动申请权限;
private void checkPermission() {
//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//用户已经拒绝过一次,再次弹出权限申请对话框需要给用户一个解释
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission
.WRITE_EXTERNAL_STORAGE)) {
// Toast.makeText(this, "请开通相关权限,否则无法正常使用本应用!", Toast.LENGTH_SHORT).show();
}
//申请权限READ_EXTERNAL_STORAGE
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE);
} else {//已经授权
photoDialogPop();//
DLog.e("AddRepairAct","TAG_SERVICE"+ "checkPermission: 已经授权!");
}
}
//系统方法,从requestPermissions()方法回调结果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//确保是我们的请求
if(requestCode == REQUEST_WRITE_EXTERNAL_STORAGE){
mHandler.post(new Runnable() {
@Override
public void run() {
photoDialogPop();
}
});
}
}
2.在res中添加储存目录的xml文件 file_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<!--
1、name对应的属性值,开发者可以自由定义;
2、path对应的属性值,当前external-path标签下的相对路径
比如:/storage/emulated/0/92Recycle-release.apk
sdcard路径:/storage/emulated/0(WriteToReadActivity.java:176)
at cn.teachcourse.nougat.WriteToReadActivity.onClick(WriteToReadActivity.java:97)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22265)
相对路径:/
-->
<!--1、对应内部内存卡根目录:Context.getFileDir()-->
<files-path
name="int_root"
path="/" />
<!--2、对应应用默认缓存根目录:Context.getCacheDir()-->
<cache-path
name="app_cache"
path="/" />
<!--3、对应外部内存卡根目录:Environment.getExternalStorageDirectory()-->
<external-path
name="ext_root"
path="pictures/" />
<!--4、对应外部内存卡根目录下的APP公共目录:Context.getExternalFileDir(String)-->
<external-files-path
name="ext_pub"
path="/" />
<!--5、对应外部内存卡根目录下的APP缓存目录:Context.getExternalCacheDir()-->
<external-cache-path
name="ext_cache"
path="/" />
<root-path //系统目录 必须存在
name="root_path"
path="." />
</paths>
3.配置文件AndroidManifest.xml 中添加 provider 声明
<provider android:name="android.support.v4.content.FileProvider" android:authorities="cn.hand.tech.provider3" //包名+provider(任意) android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_provider" />//上一步添加的xml </provider>
至此系统文件的调用和存储 条件配置完成。
以下是 系统文件的调用问题
1.获取路径
public static String getAppSdcardDir() { if (Environment.getExternalStorageState().equalsIgnoreCase( Environment.MEDIA_MOUNTED)) { return Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + APP_PATH; } else { return BApplication.mContext.getCacheDir().getPath() + "/" + APP_PATH; } }
2.通过路径获取图片或文件
在res中values的String 文件里面:<string name="authorities">cn.hand.tech.provider3</string>//必须和manifest 中provider 一致
Uri uriForFile = FileProvider.getUriForFile(this, context.getResources().getString(R.string.authorities),new File(path));//uri 必须这样获取 不然会报错 Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), uriForFile);
3.图片文件的存储
// 首先保存图片 String dirPath = FileUtils.getAppSdcardDir() + "/" + "bg_image" + "/"; File dirFile = new File(dirPath); if (!dirFile.exists()) { dirFile.mkdirs(); } String fileName = System.currentTimeMillis() + ".jpg"; final File file = new File(dirFile, fileName); FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);//图片简单压缩 会有失真 fos.flush(); fos.close();