小編給大家分享一下如何解決Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)建站長期為數(shù)千家客戶提供的網(wǎng)站建設服務,團隊從業(yè)經(jīng)驗10年,關注不同地域、不同群體,并針對不同對象提供差異化的產(chǎn)品和服務;打造開放共贏平臺,與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為霸州企業(yè)提供專業(yè)的成都網(wǎng)站設計、成都做網(wǎng)站,霸州網(wǎng)站改版等技術服務。擁有10余年豐富建站經(jīng)驗和眾多成功案例,為您定制開發(fā)。
本文實例為大家分享了Android調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題的解決方法,具體內(nèi)容如下
內(nèi)容:手機系統(tǒng)的裁剪介紹,7.0調(diào)用相機崩潰解決
代碼如下:
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系統(tǒng)解決拍照的問題 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); //啟動相機(帶返回結(jié)果) startActivityForResult(intent, TAKE_PHOTO); } /** * 啟動系統(tǒng)圖片選擇 */ private void selectFromLocal() { Intent intent = new Intent(Intent.ACTION_PICK, null); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*"); //返回數(shù)據(jù)為被選照片的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; } } } /** * 設置裁剪參數(shù),開始裁剪 * @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); } /** * 動態(tài)權限請求 * * @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) {//檢測當前權限是否已經(jīng)被允許 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調(diào)用系統(tǒng)圖片裁剪限定尺寸及7.0照相問題”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注創(chuàng)新互聯(lián)行業(yè)資訊頻道!