最近做项目中涉及到了图片相关功能 ,在使用安卓6.0手机及7.1手机拍照时,遇到了因权限及文件管理导致程序崩溃等问题。
刚好把功能修改完,把代码简单地贴一下,方便以后使用。
—-主界面 代码 ——
public class MainActivity extends AppCompatActivity {
//拍照按钮
private Button take_photo;
//显示裁剪后的图片
private ImageView photo_iv;
private static final int PERMISSIONS_FOR_TAKE_PHOTO = 10;
//图片文件路径
private String picPath;
//图片对应Uri
private Uri photoUri;
//拍照对应RequestCode
public static final int SELECt_PIC_BY_TACK_PHOTO = 1;
//裁剪图片
private static final int CROP_PICTURE = 3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
take_photo = (Button) findViewById(R.id.take_photo);
photo_iv = (ImageView) findViewById(R.id.photo_iv);
take_photo.setonClickListener(new View.onClickListener() {
@Override
public void onClick(View view) {
//小于6.0版本直接操作
if (Build.VERSION.SDK_INT < 23) {
takePictures();
} else {
//6.0以后权限处理
permissionForM();
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == SELECT_PIC_BY_TACK_PHOTO) {
String[] pojo = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(photoUri, pojo, null, null, null);
if (cursor != null) {
int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]);
cursor.moveToFirst();
picPath = cursor.getString(columnIndex);
if (Build.VERSION.SDK_INT < 14) {
cursor.close();
}
}
if (picPath != null && (picPath.endsWith(".png") || picPath.endsWith(".PNG") || picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) {
photoUri = Uri.fromFile(new File(picPath));
if (Build.VERSION.SDK_INT > 23) {
photoUri = FileProvider.getUriForFile(this, "com.innopro.bamboo.fileprovider", new File(picPath));
cropForN(picPath, CROP_PICTURE);
} else {
startPhotoZoom(photoUri, CROP_PICTURE);
}
} else {
//错误提示
}
}
if (requestCode == CROP_PICTURE) {
if (photoUri != null) {
Bitmap bitmap = BitmapFactory.decodeFile(picPath);
if (bitmap != null) {
photo_iv.setImageBitmap(bitmap);
}
}
}
}
}
private void takePictures() {
//执行拍照前,应该先判断SD卡是否存在
String SDState = Environment.getExternalStorageState();
if (SDState.equals(Environment.MEDIA_MOUNTED)) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
ContentValues values = new ContentValues();
photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO);
} else {
Toast.makeText(this, "手机未插入内存卡", Toast.LENGTH_LONG).show();
}
}
private void startPhotoZoom(Uri uri,
int REQUE_CODE_CROP) {
int dp = 500;
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image
private void cropForN(String imagePath, int REQUE_CODE_CROP) {
Uri cropUri = getImageContentUri(new File(imagePath));
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(cropUri, "image
private void permissionForM() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSIONS_FOR_TAKE_PHOTO);
} else {
takePictures();
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSIONS_FOR_TAKE_PHOTO) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
takePictures();
}
return;
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
–主界面布局——–
–AndroidManifest.xml添加provider——–
–资源文件下添加xml文件夹及file_paths文件——–
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



