日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > windows >内容正文

windows

调用系统相机和相册,并且裁剪成圆形图片(解决6.0,7.0,8.0版本问题)

發(fā)布時間:2023/12/18 windows 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 调用系统相机和相册,并且裁剪成圆形图片(解决6.0,7.0,8.0版本问题) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

之前寫過一篇博客,那篇博客對7.0手機裁剪圖片的問題沒有進行解決,現(xiàn)在對之前的那篇博客進行補充,解決了Android6.0,7.0,8.0版本問題,不僅可以調(diào)用相冊,相機,還可以將圖片保存到本地,并且裁剪成圓形圖片

必要的權(quán)限:

<uses-permission android:name="android.permission.CAMERA" /><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.INTERNET" />

在res文件下建一個文件夾xml ?xml文件夾下一個文件file_path

<?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="http://schemas.android.com/apk/res/android"><external-pathname="external_storage_root"path="." /> </paths>

AndroidManifest:

<application...<providerandroid:name="android.support.v4.content.FileProvider"android:authorities="com.example.jingqian.camera.fileprovider"android:grantUriPermissions="true"android:exported="false"><meta-dataandroid:name="android.support.FILE_PROVIDER_PATHS"android:resource="@xml/file_path"></meta-data></provider></application>

MainActivity布局:

<ImageButtonandroid:id="@+id/iv_head"android:layout_width="120dp"android:layout_height="120dp"android:layout_centerHorizontal="true"android:layout_marginTop="10dp"android:background="@null"android:scaleType="fitXY"android:src="@drawable/ic_launcher_background" />

PopupWindow布局:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:background="#00000000"android:gravity="bottom"android:orientation="vertical"android:padding="5dip" ><Buttonandroid:id="@+id/btn_picture"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/photo_gallery_selector"android:paddingBottom="10dip"android:paddingTop="10dip"android:text="圖庫"android:textSize="16sp" /><TextViewandroid:layout_width="match_parent"android:layout_height="0.5dip"android:background="#DAD9DB" /><Buttonandroid:id="@+id/btn_photo"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/photo_camera_selector"android:paddingBottom="10dip"android:paddingTop="10dip"android:text="拍照"android:textSize="16sp" /><Buttonandroid:id="@+id/btn_cancle"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="5dip"android:background="@drawable/photo_cancel_selector"android:paddingBottom="10dip"android:paddingTop="10dip"android:text="取消"android:textSize="16sp" /></LinearLayout>

MainAtivity中代碼:

//這是popupWindow中的相冊按鈕,相機按鈕,取消按鈕private Button btn_picture, btn_photo, btn_cancle;//頭像控件private ImageButton ivHead;private Bitmap head;// 頭像Bitmap@SuppressLint("SdCardPath")private static String path = "/sdcard/myHead/";// sd路徑private Uri imageUri;private Uri uri;

onCreate方法:

DongTaiShare();ivHead = (ImageButton) findViewById(R.id.iv_head);ivHead.setOnClickListener(this);

頭像的點擊事件里:

//得到PopupWindow的視圖View view = getLayoutInflater().inflate(R.layout.photo_choose_dialog, null);//給彈框設(shè)置樣式final Dialog dialog = new Dialog(this, R.style.transparentFrameWindowStyle);dialog.setContentView(view, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));Window window = dialog.getWindow();// 設(shè)置顯示動畫window.setWindowAnimations(R.style.main_menu_animstyle);WindowManager.LayoutParams wl = window.getAttributes();wl.x = 0;wl.y = getWindowManager().getDefaultDisplay().getHeight();// 以下這兩句是為了保證按鈕可以水平滿屏wl.width = ViewGroup.LayoutParams.MATCH_PARENT;wl.height = ViewGroup.LayoutParams.WRAP_CONTENT;// 設(shè)置顯示位置dialog.onWindowAttributesChanged(wl);// 設(shè)置點擊外圍解散dialog.setCanceledOnTouchOutside(true);dialog.show();btn_picture = (Button) window.findViewById(R.id.btn_picture);btn_photo = (Button) window.findViewById(R.id.btn_photo);btn_cancle = (Button) window.findViewById(R.id.btn_cancle);btn_picture.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//調(diào)用系統(tǒng)相冊Intent intent1 = new Intent(Intent.ACTION_PICK, null);intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");startActivityForResult(intent1, 1);dialog.dismiss();}});btn_photo.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//調(diào)用系統(tǒng)相機Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);File file = new File(Environment.getExternalStorageDirectory(), "head.jpg"); //判斷手機版本號,這里是7.0以上的if (Build.VERSION.SDK_INT >= 24) {uri = FileProvider.getUriForFile(MainActivity.this, "com.example.jingqian.camera.fileprovider", file);} else {uri = Uri.fromFile(file); //7.0以下}intent2.putExtra(MediaStore.EXTRA_OUTPUT, uri);startActivityForResult(intent2, 5);dialog.dismiss();}});btn_cancle.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {dialog.dismiss();}});

動態(tài)權(quán)限

private void DongTaiShare() {if (Build.VERSION.SDK_INT >= 23) {String[] mPermissionList = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.CALL_PHONE, Manifest.permission.READ_LOGS, Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.SET_DEBUG_APP, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.GET_ACCOUNTS, Manifest.permission.WRITE_APN_SETTINGS, Manifest.permission.CAMERA};ActivityCompat.requestPermissions(this, mPermissionList, 123);}}

在MainActivity中的一些方法

private void startPhotoZoom(Uri uri) {File CropPhoto = new File(getExternalCacheDir(), "Crop.jpg");//這個是創(chuàng)建一個截取后的圖片路徑和名稱。try {if (CropPhoto.exists()) {CropPhoto.delete();}CropPhoto.createNewFile();} catch (IOException e) {e.printStackTrace();}imageUri = Uri.fromFile(CropPhoto);Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //添加這一句表示對目標(biāo)應(yīng)用臨時授權(quán)該Uri所代表的文件}intent.putExtra("crop", "true");intent.putExtra("scale", true);intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);//輸出的寬高intent.putExtra("outputX", 300);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 detectionstartActivityForResult(intent, 6);//這里的RESULT_REQUEST_CODE是在startActivityForResult里使用的返回值。}protected void onActivityResult(int requestCode, int resultCode, Intent data) {switch (requestCode) {case 1:if (resultCode == RESULT_OK) {cropPhoto(data.getData());// 裁剪圖片}break;case 2:if (resultCode == RESULT_OK) { // File file = new File(Environment.getExternalStorageDirectory() + "/head.jpg"); // Uri uri = Uri.fromFile(file);File file = new File(Environment.getExternalStorageDirectory(), "head.jpg");Uri uri = FileProvider.getUriForFile(MainActivity.this, "com.example.jingqian.camera.fileprovider", file);cropPhoto(uri);// 裁剪圖片// Bitmap bitmap= data.getParcelableExtra("data"); // ivHead.setImageBitmap(bitmap);}break;case 3:if (data != null) {Bundle extras = data.getExtras();head = extras.getParcelable("data");if (head != null) {/*** 上傳服務(wù)器代碼*/setPicToView(head);// 保存在SD卡中ivHead.setImageBitmap(toRoundBitmap(head));// 用ImageView顯示出來}}break;case 5:startPhotoZoom(uri);break;case 6:if (resultCode == RESULT_OK) {try {Log.e("=====",imageUri.toString());Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));ivHead.setImageBitmap(toRoundBitmap(bitmap));} catch (FileNotFoundException e) {e.printStackTrace();}}break;default:break;}super.onActivityResult(requestCode, resultCode, data);};/*** 調(diào)用系統(tǒng)的裁剪** @param uri*/public void cropPhoto(Uri uri) {Intent intent = new Intent("com.android.camera.action.CROP");intent.setDataAndType(uri, "image/*");intent.putExtra("crop", "true");// aspectX aspectY 是寬高的比例intent.putExtra("aspectX", 1);intent.putExtra("aspectY", 1);// outputX outputY 是裁剪圖片寬高intent.putExtra("outputX", 150);intent.putExtra("outputY", 150);intent.putExtra("return-data", true);startActivityForResult(intent, 3);}private void setPicToView(Bitmap mBitmap) {String sdStatus = Environment.getExternalStorageState();if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 檢測sd是否可用return;}FileOutputStream b = null;File file = new File(path);file.mkdirs();// 創(chuàng)建文件夾String fileName = path + "head.jpg";// 圖片名字try {b = new FileOutputStream(fileName);mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把數(shù)據(jù)寫入文件} catch (FileNotFoundException e) {e.printStackTrace();} finally {try {// 關(guān)閉流b.flush();b.close();} catch (IOException e) {e.printStackTrace();}}}/*** 把bitmap轉(zhuǎn)成圓形*/public Bitmap toRoundBitmap(Bitmap bitmap) {int width = bitmap.getWidth();int height = bitmap.getHeight();int r = 0;// 取最短邊做邊長if (width < height) {r = width;} else {r = height;}// 構(gòu)建一個bitmapBitmap backgroundBm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);// new一個Canvas,在backgroundBmp上畫圖Canvas canvas = new Canvas(backgroundBm);Paint p = new Paint();// 設(shè)置邊緣光滑,去掉鋸齒p.setAntiAlias(true);RectF rect = new RectF(0, 0, r, r);// 通過制定的rect畫一個圓角矩形,當(dāng)圓角X軸方向的半徑等于Y軸方向的半徑時,// 且都等于r/2時,畫出來的圓角矩形就是圓形canvas.drawRoundRect(rect, r / 2, r / 2, p);// 設(shè)置當(dāng)兩個圖形相交時的模式,SRC_IN為取SRC圖形相交的部分,多余的將被去掉p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));// canvas將bitmap畫在backgroundBmp上canvas.drawBitmap(bitmap, null, rect, p);return backgroundBm;}

還有點擊頭像時popupWindow時從下往上滑出的,所以還要添加一些動畫,

在res文件下建anim文件夾 ? 文件夾下兩個文件photo_dialog_in_anim.xml ? 和 ??photo_dialog_out_anim.xml

photo_dialog_in_anim.xml :

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ><translateandroid:duration="500"android:fromXDelta="0"android:fromYDelta="1000"android:toXDelta="0"android:toYDelta="0" /></set>

photo_dialog_out_anim.xml:

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" ><translateandroid:duration="500"android:fromXDelta="0"android:fromYDelta="0"android:toXDelta="0"android:toYDelta="1000" /></set>

在style下設(shè)置:

<style name="main_menu_animstyle"><item name="android:windowEnterAnimation">@anim/photo_dialog_in_anim</item><item name="android:windowExitAnimation">@anim/photo_dialog_out_anim</item></style>

這樣就可以完美實現(xiàn)調(diào)用相冊和相機并且裁剪圖片了

May everyone be happy every day and everything go well!

?

總結(jié)

以上是生活随笔為你收集整理的调用系统相机和相册,并且裁剪成圆形图片(解决6.0,7.0,8.0版本问题)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。