Android调用系统图片裁剪限定尺寸及7.0照相问题的解决方法
作者:Mr_Leixiansheng
这篇文章主要介绍了Android调用系统图片裁剪限定尺寸,及7.0照相问题的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了Android调用系统图片裁剪限定尺寸及7.0照相问题的解决方法,供大家参考,具体内容如下
内容:手机系统的裁剪介绍,7.0调用相机崩溃解决
代码如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.leixiansheng.cutphoto.MainActivity"> <Button android:id="@+id/btn_select_from_local" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="cut"/> <ImageView android:id="@+id/iv_local" android:layout_gravity="center_horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
java:
package com.example.leixiansheng.cutphoto; import android.Manifest; import android.annotation.SuppressLint; import android.content.Intent; import android.content.pm.PackageManager; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.net.Uri; import android.os.Environment; import android.os.StrictMode; import android.provider.MediaStore; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v4.content.ContextCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import butterknife.OnClick; public class MainActivity extends AppCompatActivity { @BindView(R.id.btn_select_from_local) Button mBtnSelectFromLocal; @BindView(R.id.btn_take_photo) Button mBtnTake; @BindView(R.id.iv_local) ImageView mIvLocal; private static final String TAG = MainActivity.class.getSimpleName(); private static final String IMAGE_FILE_LOCATION = "file:///" + Environment.getExternalStorageDirectory().getPath() + "/temp.jpg"; private static final int CHOOSE_PICTURE = 1; private static final int CROP_PICTURE = 2; private static final int REQUEST_CODE_OK = 3; private static final int TAKE_PHOTO = 4; private Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION); private PermissionListener mListener; @SuppressLint("NewApi") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); // android 7.0系统解决拍照的问题 StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); StrictMode.setVmPolicy(builder.build()); builder.detectFileUriExposure(); } @OnClick({R.id.btn_select_from_local, R.id.btn_take_photo}) public void onClick(final View view) { requestRunTimePermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.CAMERA}, new PermissionListener() { @Override public void onGranted() { switch (view.getId()) { case R.id.btn_select_from_local: selectFromLocal(); break; case R.id.btn_take_photo: takePhoto(); break; } } @Override public void onDenied(List<String> deniedPermission) { Toast.makeText(MainActivity.this, "权限不足", Toast.LENGTH_SHORT).show(); } }); } /** * 启动系相机 */ private void takePhoto() { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //启动相机(带返回结果) startActivityForResult(intent, TAKE_PHOTO); } /** * 启动系统图片选择 */ private void selectFromLocal() { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); //返回数据为被选照片的Uri startActivityForResult(intent, CHOOSE_PICTURE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d(TAG, resultCode + ""); if (resultCode == RESULT_OK) { switch (requestCode) { case CHOOSE_PICTURE: startPhotoZoom(data.getData()); break; case CROP_PICTURE: // 取得裁剪后的图片 try { mIvLocal.setImageBitmap(BitmapFactory.decodeStream( getContentResolver().openInputStream(imageUri) )); } catch (FileNotFoundException e) { e.printStackTrace(); } break; case TAKE_PHOTO: startPhotoZoom(imageUri); break; default: break; } } } /** * 设置裁剪参数,开始裁剪 * @param uri */ public void startPhotoZoom(Uri uri) { Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面这个crop=true是设置在开启的Intent中设置显示的VIEW可裁剪 intent.putExtra("crop", "true"); intent.putExtra("scale", true); intent.putExtra("aspectX", 2); //裁剪框 X 比值 intent.putExtra("aspectY", 1); //裁剪框 Y 比值 intent.putExtra("outputX", 600); //裁剪后输出宽度 intent.putExtra("outputY", 300); //裁剪后输出高度 intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //输出路径 intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); // no face detection startActivityForResult(intent, CROP_PICTURE); } /** * 动态权限请求 * * @param permissions */ public void requestRunTimePermission(String[] permissions, PermissionListener listener) { mListener = listener; List<String> permissionList = new ArrayList<>(); for (String permission : permissions) { if (ContextCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {//检测当前权限是否已经被允许 permissionList.add(permission);//没有则添加进集合 } } if (!permissionList.isEmpty()) {//检测权限集合中是否有未申请的权限 ActivityCompat.requestPermissions(this, permissionList.toArray(new String[permissionList.size()]), REQUEST_CODE_OK); } else { //所有权限都通过了 mListener.onGranted(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode) { case REQUEST_CODE_OK: if (grantResults.length > 0) { List<String> deniedPermission = new ArrayList<>(); for (int i = 0; i < grantResults.length; i++) { int grantResult = grantResults[i]; String permission = permissions[i]; if (grantResult != PackageManager.PERMISSION_GRANTED) { deniedPermission.add(permission); } } if (!deniedPermission.isEmpty()) { mListener.onDenied(deniedPermission); } else mListener.onGranted(); } break; default: break; } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。