您好,登錄后才能下訂單哦!
小編給大家分享一下如何解決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; } } }
以上是“如何解決Android調用系統圖片裁剪限定尺寸及7.0照相問題”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。