近期项目的需求是需要去调用相机相册去拍照获取图片保存到服务器
公司的测试机试Android6.0以下的 所以之前调用相机一点都没问题 可是跑在Android8.0+的手机上 调用相机相册就会出现错误
暴露的Url
所以在博客找到了相关文章,结合自身的需求 总结了这篇文章 希望能给您带来帮助
Android 6.0动态访问权限(相机为例)
android:resource="@xml/file_paths" /> file_paths 一样的文件名
4.file_paths文件
//文件
private File file;
//相机请求码
private final int REQUEST_CAMERA = 1;
private void useCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
file = new File(Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/test/" + System.currentTimeMillis() + ".jpg");
file.getParentFile().mkdirs();
//改变Uri com.xykj.customview.fileprovider注意和xml中的一致
Uri uri = FileProvider.getUriForFile(this, "com.miaoyin.zcda.fileprovider", file);
//添加权限
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQUEST_CAMERA);
}
public void applyWritePermission() {
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= 23) {
int check = ContextCompat.checkSelfPermission(this, permissions[0]);
// 权限是否已经 授权 GRANTED---授权 DINIED---拒绝
if (check == PackageManager.PERMISSION_GRANTED) {
//调用相机
useCamera();
} else {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
} else {
useCamera();
}
}
/**
* 拍照的,照片路径
*/
public void jsPath() {
//获取SD卡安装状态
String state = Environment.getExternalStorageState();
if (state.equals(Environment.MEDIA_MOUNTED)) {
//设置图片保存路径
photoPath = SAVED_IMAGE_PATH + "/" + System.currentTimeMillis() + ".png";
File imageDir = new File(photoPath);
if (!imageDir.exists()) {
try {
//根据一个 文件地址生成一个新的文件用来存照片
imageDir.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
takePhotoByMethod1();
} else {
Toast.makeText(SubmitCarryCampLicenseActivity.this, "SD卡未插入", Toast.LENGTH_SHORT).show();
}
}
private void takePhotoByMethod1() {
//实例化intent,指向摄像头
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//根据路径实例化图片文件
File photoFile = new File(photoPath);
//设置拍照后图片保存到文件中
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
//启动拍照activity并获取返回数据
startActivityForResult(intent, REQUEST_CAMERA);
}
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
// Log.e("TAG", "---------" + FileProvider.getUriForFile(this, "com.xykj.customview.fileprovider", file));
//在手机相册中显示刚拍摄的图片
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(file);
String path = contentUri.getPath();
Log.e("TAGSSSSSSSSS", contentUri.toString());
//文件的路径
String absolutePath = file.getAbsolutePath();
//文件的名字
String parent = file.getName();
Log.e("TAGQQQQQQQQQ", parent + "===" + absolutePath);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
//设置图片
sdPic.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
}else{
File photoFile = new File(photoPath);
if (photoFile.exists()) {
//通过图片地址将图片加载到bitmap里面
Bitmap bm = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
//将拍摄的照片显示到界面上
//这是返回来的路径
// results.confirm(pathsa);
Toast.makeText(SubmitCarryCampLicenseActivity.this, "通过", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SubmitCarryCampLicenseActivity.this, "图片文件不存在", Toast.LENGTH_LONG).show();
}
}
}
if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
useCamera();
} else {
// 没有获取 到权限,从新请求,或者关闭app
Toast.makeText(this, "需要存储权限", Toast.LENGTH_SHORT).show();
}
public void showPhoto() {
Intent it1 = new Intent(Intent.ACTION_PICK);
it1.setType("image/*");
mPhoto = 1;
startActivityForResult(it1, 200);
}
if (requestCode == 200 && resultCode == RESULT_OK) {
Intent it = new Intent("com.android.camera.action.CROP");
it.putExtra("crop", true);
Uri data1 = data.getData();
it.setDataAndType(data1, "image/*");
it.putExtra("aspactX", 1);
it.putExtra("aspactY", 1);
it.putExtra("outputX", 80);
it.putExtra("outputY", 80);
it.putExtra("return-data", true);
startActivityForResult(it, 300);
}
到这里8.0+调用相册就完成了
希望能对您有所帮助