国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現從相冊截圖的功能

2020-04-11 10:58:37
字體:
來源:轉載
供稿:網友

在這篇文章中,我將向大家展示如何從相冊截圖。
先看看效果圖:

上一篇文章中,我就拍照截圖這一需求進行了詳細的分析,試圖讓大家了解Android本身的限制,以及我們應當采取的實現方案。大家可以回顧一下:Android實現拍照截圖功能

根據我們的分析與總結,圖片的來源有拍照和相冊,而可采取的操作有

  • 使用Bitmap并返回數據
  • 使用Uri不返回數據

前面我們了解到,使用Bitmap有可能會導致圖片過大,而不能返回實際大小的圖片,我將采用大圖Uri,小圖Bitmap的數據存儲方式。

我們將要使用到URI來保存拍照后的圖片:

privatestatic final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp fileUri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap

不難知道,我們從相冊選取圖片的Action為Intent.ACTION_GET_CONTENT。

根據我們上一篇博客的分析,我準備好了兩個實例的Intent。

一、從相冊截大圖:

Intent intent = newIntent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop","true");intent.putExtra("aspectX",2);intent.putExtra("aspectY",1);intent.putExtra("outputX",600);intent.putExtra("outputY",300);intent.putExtra("scale",true);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, CHOOSE_BIG_PICTURE);

二、從相冊截小圖

Intent intent = newIntent(Intent.ACTION_GET_CONTENT, null);intent.setType("image/*");intent.putExtra("crop","true");intent.putExtra("aspectX",2);intent.putExtra("aspectY",1);intent.putExtra("outputX",200);intent.putExtra("outputY",100);intent.putExtra("scale",true);intent.putExtra("return-data",true);intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());intent.putExtra("noFaceDetection",true);// no face detectionstartActivityForResult(intent, CHOOSE_SMALL_PICTURE);

三、對應的onActivityResult可以這樣處理返回的數據

switch(requestCode) {caseCHOOSE_BIG_PICTURE:  Log.d(TAG,"CHOOSE_BIG_PICTURE: data = " + data);//it seems to be null  if(imageUri != null){    Bitmap bitmap = decodeUriAsBitmap(imageUri);//decode bitmap    imageView.setImageBitmap(bitmap);  }  break;caseCHOOSE_SMALL_PICTURE:  if(data != null){    Bitmap bitmap = data.getParcelableExtra("data");    imageView.setImageBitmap(bitmap);  }else{    Log.e(TAG,"CHOOSE_SMALL_PICTURE: data = " + data);  }  break;default:  break;}privateBitmap decodeUriAsBitmap(Uri uri){  Bitmap bitmap = null;  try{    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));  }catch(FileNotFoundException e) {    e.printStackTrace();    returnnull;  }  returnbitmap;}

以上就是Android實現拍照截圖功能的方法,希望對大家的學習有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江川县| 吕梁市| 岑溪市| 通河县| 马尔康县| 襄樊市| 长沙县| 山西省| 喀喇沁旗| 资中县| 莱西市| 慈利县| 尼勒克县| 新兴县| 雅安市| 古浪县| 格尔木市| 榆中县| 林周县| 鹤山市| 东兰县| 长寿区| 虹口区| 会东县| 满洲里市| 从江县| 防城港市| 苍梧县| 昌乐县| 固始县| 阿尔山市| 沅陵县| 内乡县| 兴国县| 南靖县| 前郭尔| 新和县| 沈丘县| 含山县| 宁陵县| 旌德县|