Android 7.0 获取相机拍照图片

来源:互联网 发布:鸮和猫头鹰区别知乎 编辑:程序博客网 时间:2024/06/04 19:00

新建/res/xml/file_paths:

<?xml version="1.0" encoding="utf-8"?><paths xmlns:android="http://schemas.android.com/apk/res/android">    <external-path name="external_files" path="."/></paths>
配置manifest文件

<provider    android:name="android.support.v4.content.FileProvider"    android:authorities="com.hunuo.yohoo.provider" //com.hunuo.yohoo=包名    android:exported="false"    android:grantUriPermissions="true">    <meta-data        android:name="android.support.FILE_PROVIDER_PATHS"        android:resource="@xml/provider_paths"/></provider>
修改uri和putExtra参数

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);if (cameraIntent.resolveActivity(UserManageActivity.this.getPackageManager()) != null) {    filePhoto = new File(path, getTime()+".jpg");    Uri photoURI = FileProvider.getUriForFile(UserManageActivity.this, "项目包名.provider", filePhoto);    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI); //Uri.fromFile(tempFile)    startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);}
剪切图片

    private void crop(file filePhoto) {//        cropImagePath = file.getAbsolutePath();        Intent intent = new Intent("com.android.camera.action.CROP");        intent.setDataAndType(getImageContentUri(filePhoto), "image/*");        intent.putExtra("crop", "true");        intent.putExtra("aspectX", 300);        intent.putExtra("aspectY", 300);        intent.putExtra("outputX", 300);        intent.putExtra("outputY", 300);        intent.putExtra("scale", true);        intent.putExtra("return-data", false);        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(filePhoto));        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());        intent.putExtra("noFaceDetection", true);        startActivityForResult(intent, RESULT_REQUEST_CODE);    }

使用时间命名图片

public String getTime(){    long time=System.currentTimeMillis()/1000;//获取系统时间的10位的时间戳    String  str=String.valueOf(time);    return str;}

0 0
原创粉丝点击