大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
小编给大家分享一下如何解决Android调用系统图片裁剪限定尺寸及7.0照相问题,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!
创新互联建站长期为数千家客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为霸州企业提供专业的成都网站设计、成都做网站,霸州网站改版等技术服务。拥有10余年丰富建站经验和众多成功案例,为您定制开发。
本文实例为大家分享了Android调用系统图片裁剪限定尺寸及7.0照相问题的解决方法,具体内容如下
内容:手机系统的裁剪介绍,7.0调用相机崩溃解决
代码如下:
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(ListdeniedPermission) { 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 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 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; } } }
以上是“如何解决Android调用系统图片裁剪限定尺寸及7.0照相问题”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!