Android實(shí)現(xiàn)從相冊(cè)截圖的功能
在這篇文章中,我將向大家展示如何從相冊(cè)截圖。
先看看效果圖:


上一篇文章中,我就拍照截圖這一需求進(jìn)行了詳細(xì)的分析,試圖讓大家了解Android本身的限制,以及我們應(yīng)當(dāng)采取的實(shí)現(xiàn)方案。大家可以回顧一下:Android實(shí)現(xiàn)拍照截圖功能
根據(jù)我們的分析與總結(jié),圖片的來(lái)源有拍照和相冊(cè),而可采取的操作有
- 使用Bitmap并返回?cái)?shù)據(jù)
- 使用Uri不返回?cái)?shù)據(jù)
前面我們了解到,使用Bitmap有可能會(huì)導(dǎo)致圖片過(guò)大,而不能返回實(shí)際大小的圖片,我將采用大圖Uri,小圖Bitmap的數(shù)據(jù)存儲(chǔ)方式。
我們將要使用到URI來(lái)保存拍照后的圖片:
private static final String IMAGE_FILE_LOCATION = "file:///sdcard/temp.jpg";//temp file Uri imageUri = Uri.parse(IMAGE_FILE_LOCATION);//The Uri to store the big bitmap
不難知道,我們從相冊(cè)選取圖片的Action為Intent.ACTION_GET_CONTENT。
根據(jù)我們上一篇博客的分析,我準(zhǔn)備好了兩個(gè)實(shí)例的Intent。
一、從相冊(cè)截大圖:
Intent
intent = new
Intent(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 detection
startActivityForResult(intent,
CHOOSE_BIG_PICTURE);
二、從相冊(cè)截小圖
Intent
intent = new
Intent(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 detection
startActivityForResult(intent,
CHOOSE_SMALL_PICTURE);
三、對(duì)應(yīng)的onActivityResult可以這樣處理返回的數(shù)據(jù)
switch
(requestCode) {
case
CHOOSE_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;
case
CHOOSE_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;
}
private
Bitmap decodeUriAsBitmap(Uri uri){
Bitmap
bitmap = null;
try
{
bitmap
= BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));
}
catch
(FileNotFoundException e) {
e.printStackTrace();
return
null;
}
return
bitmap;
}
以上就是Android實(shí)現(xiàn)拍照截圖功能的方法,希望對(duì)大家的學(xué)習(xí)有所幫助。
- Native.js獲取監(jiān)聽(tīng)開(kāi)關(guān)等操作Android藍(lán)牙設(shè)備實(shí)例代碼
- native.js獲取手機(jī)硬件基本信息實(shí)例代碼android版
- Dcloud的native.js直接撥打電話Android實(shí)例代碼
- DCloud的native.js調(diào)用系統(tǒng)分享實(shí)例Android版代碼
- Android中通過(guò)view方式獲取當(dāng)前Activity的屏幕截圖實(shí)現(xiàn)方法
- Android中如何獲取視頻文件的截圖、縮略圖
- Android模擬器中窗口截圖存成文件實(shí)現(xiàn)思路及代碼
- 詳解有關(guān)Android截圖與錄屏功能的學(xué)習(xí)
- Android實(shí)現(xiàn)截圖和分享功能的代碼
- Android獲取常用輔助方法(獲取屏幕高度、寬度、密度、通知欄高度、截圖)
- Android實(shí)現(xiàn)拍照截圖功能
- android截圖事件監(jiān)聽(tīng)的原理與實(shí)現(xiàn)
- Android屏幕及view的截圖實(shí)例詳解
- Android截屏截圖的幾種方法總結(jié)
- Android實(shí)現(xiàn)截圖分享qq 微信功能
- Android 中WebView 截圖的實(shí)現(xiàn)方式
- Android App內(nèi)監(jiān)聽(tīng)截圖加二維碼功能代碼
- Android 5.0及以上編程實(shí)現(xiàn)屏幕截圖功能的方法
- Android仿銀行客戶簽名并且保存簽名的截圖文件并命名為本地時(shí)間
- Android 截圖功能源碼的分析
- Android使用WebView實(shí)現(xiàn)截圖分享功能
- Native.js屏幕截圖實(shí)例代碼
相關(guān)文章
Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)手機(jī)監(jiān)控?cái)z像頭,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-03-03
Android發(fā)送GET與POST請(qǐng)求的DEMO詳解
本篇文章是對(duì)Android發(fā)送GET與POST請(qǐng)求的DEMO進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-06-06
Android App后臺(tái)震動(dòng)的實(shí)現(xiàn)步驟詳解
這篇文章主要為大家介紹了Android App后臺(tái)震動(dòng)的實(shí)現(xiàn)步驟詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
最好用的Android省市區(qū)三級(jí)聯(lián)動(dòng)選擇效果
這篇文章主要為大家詳細(xì)介紹了最好用的Android省市區(qū)三級(jí)聯(lián)動(dòng)選擇效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
Android編程實(shí)現(xiàn)使用SoundPool播放音樂(lè)的方法
這篇文章主要介紹了Android編程實(shí)現(xiàn)使用SoundPool播放音樂(lè)的方法,較為詳細(xì)的分析說(shuō)明了SoundPool對(duì)象的使用技巧,需要的朋友可以參考下2016-01-01
Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法
這篇文章主要介紹了Android改變ExpandableListView的indicator圖標(biāo)實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了改變ExpandableListView的indicator圖標(biāo)相關(guān)步驟與實(shí)現(xiàn)技巧,涉及Android配置文件的修改,需要的朋友可以參考下2016-03-03
詳解android studio游戲搖桿開(kāi)發(fā)教程,仿王者榮耀搖桿
這篇文章主要介紹了android studio游戲搖桿開(kāi)發(fā)教程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Android自定義控件實(shí)現(xiàn)按鈕滾動(dòng)選擇效果
這篇文章主要為大家詳細(xì)介紹了Android自定義控件實(shí)現(xiàn)按鈕滾動(dòng)選擇效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07
Android自定義popupwindow實(shí)例代碼
這篇文章主要為大家詳細(xì)介紹了Android自定義popupwindow實(shí)例代碼,popupwindow彈出菜單效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Android加載大分辨率圖片到手機(jī)內(nèi)存中的實(shí)例方法
有些圖片的分辨率比較高,把它直接加載到手機(jī)內(nèi)存中之后,會(huì)導(dǎo)致堆內(nèi)存溢出的問(wèn)題,下面就講解一下Android的堆內(nèi)存以及如何在Android應(yīng)用中加載一個(gè)高分辨率的圖片的方法2013-11-11

