营销型网站建设 兼职,网站的内容和功能,公司装修便宜,手机网站 收录在某些行业#xff0c;APP可能被禁止使用存储权限#xff0c;或公司在写SDK功能#xff0c;不方便获取权限
所以需要有 无存储权限拍照方案。这里两种方案都列出里。
对于写入权限#xff0c;在高版本中#xff0c;已经废弃#xff0c; 不可用文件写入读取权限#xf…在某些行业APP可能被禁止使用存储权限或公司在写SDK功能不方便获取权限
所以需要有 无存储权限拍照方案。这里两种方案都列出里。
对于写入权限在高版本中已经废弃 不可用文件写入读取权限所以拍照功能也最好使用无权限方式如果需要获取相册 需要使用新的权限---媒体权限 业务前提 判断拍照权限
if(activity.checkSelfPermission(Manifest.permission.CAMERA) PackageManager.PERMISSION_GRANTED){
//开始拍照流程
}else{
activity.requestPermissions(new String[]{Manifest.permission.CAMERA},1001);
}
存储地址及Uri
一、无存储权限
1、创建存储路径 var filePath:String?null;if (Build.VERSION.SDK_INT Build.VERSION_CODES.N) {filePath mActivity.filesDir!!.absolutePath Environment.DIRECTORY_PICTURES File.separator;} else {filePath FileUtil.getCachePath() Environment.DIRECTORY_PICTURES File.separator;}
工具类 FileUtil.getCachePath public static String getCachePath() {File externalCacheDir context.getExternalCacheDir();if (null ! externalCacheDir) {return externalCacheDir.getAbsolutePath();}if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {return Environment.getExternalStorageDirectory().getAbsolutePath();}return context.getCacheDir().getAbsolutePath();}
2、path转Uri // 指定拍照存储位置的方式调起相机var fileName IMG_ DateFormat.format(yyyyMMdd_hhmmss,Calendar.getInstance(Locale.CHINA)) .jpg;val file File(filePath fileName);if (Build.VERSION.SDK_INT Build.VERSION_CODES.N) {val contentValues ContentValues()contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, fileName)contentValues.put(MediaStore.Images.Media.MIME_TYPE, image/jpeg)imageUri mActivity.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues)} else {imageUri Uri.fromFile(file)}
二、有存储权限
1配置provider官方适配方案
1、androidx.core.content.FileProvider 可以用v4包 也可用 Androidx看自己项目用哪个库
2、applicationId 是在build.gradle文件中定义的一个进程Id也是应用唯一id providerandroid:nameandroidx.core.content.FileProviderandroid:authorities${applicationId}.providerandroid:exportedfalseandroid:grantUriPermissionstruemeta-dataandroid:nameandroid.support.FILE_PROVIDER_PATHSandroid:resourcexml/filepaths //provider
3、在main/res/xml 路径下创建文件 filepaths.xml
paths
files-path namename pathpath /
//物理路径相当于Context.getFilesDir() /path/cache-path namename pathpath /
//物理路径相当于Context.getCacheDir() /path/external-path namename pathpath /
//物理路径相当于Environment.getExternalStorageDirectory() /path/external-files-path namename pathpath /
//物理路径相当于Context.getExternalFilesDir(String) /path/external-cache-path namename pathpath /
//物理路径相当于Context.getExternalCacheDir() /path/
/paths
代码中每一个path配置都对应一个java api例如 mActivity.getExternalFilesDir(MyFilePath) 对应external-files-path的地址: /storage/emulated/0/Android/data/{applicationId}/files/MyFilePath 后面需要自己拼接/path
getExternalFilesDir 入参可为null
4、java代码
//判断三个权限 写入写出 拍照
//android.permission.READ_EXTERNAL_STORAGEandroid.permission.WRITE_EXTERNAL_STORAGEif (activity.checkSelfPermission(Manifest.permission.CAMERA) PackageManager.PERMISSION_GRANTED) {String path FileUtil.getCachePath() File.separator cameraPath System.currentTimeMillis() .jpg;File file new File(path);if (Build.VERSION.SDK_INT Build.VERSION_CODES.N) {imageUri FileProvider.getUriForFile(activity.getApplication(), activity.getPackageName() .provider, file);activity.grantUriPermission(activity.getPackageName(), outputFileUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);} else {imageUri Uri.fromFile(file);}Intent intent new Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);outputFile.add(file);activity.startActivityForResult(intent, 1011);}
调用拍照 var intent Intent(MediaStore.ACTION_IMAGE_CAPTURE);intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);mActivity.startActivityForResult(intent, 1011);
获取结果
fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent) {if (resultCode RESULT_OK) {if(requestCode1011){
//两种方式获取结果
//1 直接使用 拍照传入的Uri 参数
//2 data.getData() 返回一个Uri}}
}