Android實(shí)現(xiàn)拍照及圖片裁剪(6.0以上權(quán)限處理及7.0以上文件管理)
最近做項(xiàng)目中涉及到了圖片相關(guān)功能 ,在使用安卓6.0手機(jī)及7.1手機(jī)拍照時(shí),遇到了因權(quán)限及文件管理導(dǎo)致程序崩潰等問題。
剛好把功能修改完,把代碼簡單地貼一下,方便以后使用。
—-主界面 代碼 ——
public class MainActivity extends AppCompatActivity { //拍照按鈕 private Button take_photo; //顯示裁剪后的圖片 private ImageView photo_iv; private static final int PERMISSIONS_FOR_TAKE_PHOTO = 10; //圖片文件路徑 private String picPath; //圖片對(duì)應(yīng)Uri private Uri photoUri; //拍照對(duì)應(yīng)RequestCode public static final int SELECT_PIC_BY_TACK_PHOTO = 1; //裁剪圖片 private static final int CROP_PICTURE = 3; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); take_photo = (Button) findViewById(R.id.take_photo); photo_iv = (ImageView) findViewById(R.id.photo_iv); take_photo.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //小于6.0版本直接操作 if (Build.VERSION.SDK_INT < 23) { takePictures(); } else { //6.0以后權(quán)限處理 permissionForM(); } } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == Activity.RESULT_OK) { if (requestCode == SELECT_PIC_BY_TACK_PHOTO) { String[] pojo = {MediaStore.Images.Media.DATA}; Cursor cursor = managedQuery(photoUri, pojo, null, null, null); if (cursor != null) { int columnIndex = cursor.getColumnIndexOrThrow(pojo[0]); cursor.moveToFirst(); picPath = cursor.getString(columnIndex); if (Build.VERSION.SDK_INT < 14) { cursor.close(); } } if (picPath != null && (picPath.endsWith(".png") || picPath.endsWith(".PNG") || picPath.endsWith(".jpg") || picPath.endsWith(".JPG"))) { photoUri = Uri.fromFile(new File(picPath)); if (Build.VERSION.SDK_INT > 23) { photoUri = FileProvider.getUriForFile(this, "com.innopro.bamboo.fileprovider", new File(picPath)); cropForN(picPath, CROP_PICTURE); } else { startPhotoZoom(photoUri, CROP_PICTURE); } } else { //錯(cuò)誤提示 } } if (requestCode == CROP_PICTURE) { if (photoUri != null) { Bitmap bitmap = BitmapFactory.decodeFile(picPath); if (bitmap != null) { photo_iv.setImageBitmap(bitmap); } } } } } /** * 拍照獲取圖片 */ private void takePictures() { //執(zhí)行拍照前,應(yīng)該先判斷SD卡是否存在 String SDState = Environment.getExternalStorageState(); if (SDState.equals(Environment.MEDIA_MOUNTED)) { Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); ContentValues values = new ContentValues(); photoUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); startActivityForResult(intent, SELECT_PIC_BY_TACK_PHOTO); } else { Toast.makeText(this, "手機(jī)未插入內(nèi)存卡", Toast.LENGTH_LONG).show(); } } /** * 圖片裁剪,參數(shù)根據(jù)自己需要設(shè)置 * * @param uri * @param REQUE_CODE_CROP */ private void startPhotoZoom(Uri uri, int REQUE_CODE_CROP) { int dp = 500; Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(uri, "image/*"); // 下面這個(gè)crop=true是設(shè)置在開啟的Intent中設(shè)置顯示的VIEW可裁剪 intent.putExtra("crop", "true"); intent.putExtra("scale", true);// 去黑邊 intent.putExtra("scaleUpIfNeeded", true);// 去黑邊 // aspectX aspectY 是寬高的比例 intent.putExtra("aspectX", 4);//輸出是X方向的比例 intent.putExtra("aspectY", 3); intent.putExtra("outputX", 600);//輸出X方向的像素 intent.putExtra("outputY", 450); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); intent.putExtra("return-data", false);//設(shè)置為不返回?cái)?shù)據(jù) startActivityForResult(intent, REQUE_CODE_CROP); } /** * 7.0以上版本圖片裁剪操作 * * @param imagePath * @param REQUE_CODE_CROP */ private void cropForN(String imagePath, int REQUE_CODE_CROP) { Uri cropUri = getImageContentUri(new File(imagePath)); Intent intent = new Intent("com.android.camera.action.CROP"); intent.setDataAndType(cropUri, "image/*"); intent.putExtra("crop", "true"); //輸出是X方向的比例 intent.putExtra("aspectX", 4); intent.putExtra("aspectY", 3); // outputX outputY 是裁剪圖片寬高 intent.putExtra("outputX", 600); intent.putExtra("outputY", 450); intent.putExtra("scale", true); intent.putExtra("return-data", false); intent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri); intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString()); intent.putExtra("noFaceDetection", true); startActivityForResult(intent, REQUE_CODE_CROP); } private Uri getImageContentUri(File imageFile) { String filePath = imageFile.getAbsolutePath(); Cursor cursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new String[]{MediaStore.Images.Media._ID}, MediaStore.Images.Media.DATA + "=? ", new String[]{filePath}, null); if (cursor != null && cursor.moveToFirst()) { int id = cursor.getInt(cursor .getColumnIndex(MediaStore.MediaColumns._ID)); Uri baseUri = Uri.parse("content://media/external/images/media"); return Uri.withAppendedPath(baseUri, "" + id); } else { if (imageFile.exists()) { ContentValues values = new ContentValues(); values.put(MediaStore.Images.Media.DATA, filePath); return getContentResolver().insert( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); } else { return null; } } } /** * 安卓6.0以上版本權(quán)限處理 */ private void permissionForM() { if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSIONS_FOR_TAKE_PHOTO); } else { takePictures(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == PERMISSIONS_FOR_TAKE_PHOTO) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { takePictures(); } return; } super.onRequestPermissionsResult(requestCode, permissions, grantResults); } }
–主界面布局——–
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout 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" tools:context="com.innopro.improve.MainActivity"> <Button android:id="@+id/take_photo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="拍照" android:textSize="18sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageView android:id="@+id/photo_iv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/take_photo" /> </android.support.constraint.ConstraintLayout>
–AndroidManifest.xml添加provider——–
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.innopro.improve.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider>
–資源文件下添加xml文件夾及file_paths文件——–
<?xml version="1.0" encoding="utf-8"?> <resources> <paths> <external-path name="camera_photos" path="" /> </paths> </resources>
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android裁剪圖片為圓形圖片的實(shí)現(xiàn)原理與代碼
- 解決Android從相冊(cè)中獲取圖片出錯(cuò)圖片卻無法裁剪問題的方法
- Android實(shí)現(xiàn)從本地圖庫/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android 7.0中拍照和圖片裁剪適配的問題詳解
- Android編程實(shí)現(xiàn)調(diào)用系統(tǒng)圖庫與裁剪圖片功能
- android文件上傳示例分享(android圖片上傳)
- Android使用post方式上傳圖片到服務(wù)器的方法
- Android實(shí)現(xiàn)本地上傳圖片并設(shè)置為圓形頭像
- Android實(shí)現(xiàn)圖片裁剪和上傳
相關(guān)文章
Android實(shí)現(xiàn)氣泡布局/彈窗效果 氣泡尖角方向及偏移量可控
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)氣泡布局/彈窗效果,可控制氣泡尖角方向及偏移量,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08Android基礎(chǔ)之startActivityForResult()的用法詳解
這篇文章主要給大家介紹了Android中startActivityForResult()的用法,文中給出了詳細(xì)的介紹與示例代碼,相信對(duì)大家的理解和學(xué)習(xí)具有一定參考借鑒價(jià)值,有需要的朋友們下面來一起看看吧。2017-01-01Recycleview實(shí)現(xiàn)無限自動(dòng)輪播
這篇文章主要為大家詳細(xì)介紹了Recycleview實(shí)現(xiàn)無限自動(dòng)輪播,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-07-07RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局
這篇文章主要為大家詳細(xì)介紹了RecyclerView仿應(yīng)用列表實(shí)現(xiàn)網(wǎng)格布局,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09Android 使用地圖時(shí)的權(quán)限請(qǐng)求方法
今天小編就為大家分享一篇Android 使用地圖時(shí)的權(quán)限請(qǐng)求方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android之采用execSQL與rawQuery方法完成數(shù)據(jù)的添刪改查操作詳解
本篇文章是對(duì)用execSQL與rawQuery方法完成數(shù)據(jù)的添刪改查操作進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06android開發(fā)教程之清除android數(shù)據(jù)緩存示例(清除本地?cái)?shù)據(jù)緩存)
android清除本地?cái)?shù)據(jù)緩存示例,主要功能清除內(nèi)/外緩存,清除數(shù)據(jù)庫,清除sharedPreference,清除files和清除自定義目錄,需要的朋友可以參考下2014-02-02