时间:2021-05-20
本文实例为大家分享了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; } }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了Android7.0上某些PopuWindow出现显示位置不正确问题的解决方法。分享给大家供大家参考,具体如下:情景描述:在andorid7.0及
前言Android7.0系统发布后,拿到能升级的nexus6P,就开始了7.0的适配。发现在Android7.0以上,在相机拍照和图片裁剪上,可能会碰到以下一些
本文实例讲述了Android实现图片拍照剪裁的方法。分享给大家供大家参考,具体如下:调用系统的裁剪工具对相册或者拍照的图片进行裁剪。startActivityf
本文实例讲述了Android编程实现调用系统图库与裁剪图片功能。分享给大家供大家参考,具体如下:在Android开发中,调用系统图库和裁剪照片是很常见的需求。相
前面照着android系统的裁剪图片的功能自己写了一个相似的工具。功能是大体上实现了,但留下了一个调用的问题:如何从我的程序调用这个裁剪工具,并且获得裁剪后的图