android圖像繪制(六)獲取本地圖片或拍照?qǐng)D片等圖片資源
先貼代碼
獲取圖片:
注釋:拍照獲取的話(huà),可以指定圖片的保存地址,在此不說(shuō)明。
CharSequence[] items = {"相冊(cè)", "相機(jī)"};
new AlertDialog.Builder(this)
.setTitle("選擇圖片來(lái)源")
.setItems(items, new OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if( which == SELECT_PICTURE ){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "選擇圖片"), SELECT_PICTURE);
}else{
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, SELECT_CAMER);
}
}
})
.create().show();
處理圖片,方法一,直接處理返回圖片:
注釋:
1、網(wǎng)上有說(shuō)明,直接處理返回的圖片是被系統(tǒng)壓縮過(guò)的,不過(guò)自己在測(cè)試的過(guò)程并沒(méi)有區(qū)別;
2、如果用戶(hù)不斷的重新獲取圖片的話(huà),必須把現(xiàn)在的Bmp內(nèi)存釋放,否則會(huì)報(bào)錯(cuò)! bmp.recycle()。
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
//選擇圖片
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
if(bmp != null)//如果不釋放的話(huà),不斷取圖片,將會(huì)內(nèi)存不夠
bmp.recycle();
bmp = BitmapFactory.decodeStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("the bmp toString: " + bmp);
imageSV.setBmp(bmp);
}else{
Toast.makeText(SetImageActivity.this, "請(qǐng)重新選擇圖片", Toast.LENGTH_SHORT).show();
}
}
處理圖片,方法二,獲得圖片的地址再處理:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
Uri uri = data.getData();
String [] proj={MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery( uri,
proj, // Which columns to return
null, // WHERE clause; which rows to return (all rows)
null, // WHERE clause selection arguments (none)
null); // Order-by clause (ascending by name)
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String path = cursor.getString(column_index);
bmp = BitmapFactory.decodeFile(path);
System.out.println("the path is :" + path);
}else{
Toast.makeText(SetImageActivity.this, "請(qǐng)重新選擇圖片", Toast.LENGTH_SHORT).show();
}
}
- Android實(shí)現(xiàn)拍照、選擇圖片并裁剪圖片功能
- Android啟動(dòng)相機(jī)拍照并返回圖片
- Android拍照保存在系統(tǒng)相冊(cè)不顯示的問(wèn)題解決方法
- Android仿微信發(fā)表說(shuō)說(shuō)實(shí)現(xiàn)拍照、多圖上傳功能
- android 拍照和上傳的實(shí)現(xiàn)代碼
- Android拍照得到全尺寸圖片并進(jìn)行壓縮
- Android應(yīng)用中拍照后獲取照片路徑并上傳的實(shí)例分享
- Android選擇圖片或拍照?qǐng)D片上傳到服務(wù)器
- Android實(shí)現(xiàn)從本地圖庫(kù)/相機(jī)拍照后裁剪圖片并設(shè)置頭像
- Android7.0實(shí)現(xiàn)拍照和相冊(cè)選取圖片功能
相關(guān)文章
Android使用SoundPool實(shí)現(xiàn)播放音頻
這篇文章主要為大家詳細(xì)介紹了Android使用SoundPool實(shí)現(xiàn)播放音頻,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05Android實(shí)現(xiàn)文件解壓帶進(jìn)度條功能
本文通過(guò)實(shí)例代碼給大家介紹了android實(shí)現(xiàn)文件解壓帶進(jìn)度條效果,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2017-08-08React Native學(xué)習(xí)之Android的返回鍵BackAndroid詳解
這篇文章主要給大家介紹了關(guān)于React Native學(xué)習(xí)之Android的返回鍵BackAndroid的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用React Native具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起看看吧。2017-10-10關(guān)于Kotlin寫(xiě)界面時(shí)諸多控件的點(diǎn)擊事件
這篇文章主要介紹了關(guān)于Kotlin寫(xiě)界面時(shí)諸多控件的點(diǎn)擊事件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03在A(yíng)ndroid中查看當(dāng)前Activity是否銷(xiāo)毀的操作
這篇文章主要介紹了在A(yíng)ndroid中查看當(dāng)前Activity是否銷(xiāo)毀的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03Android控件AppWidgetProvider使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android控件AppWidgetProvider的使用方法詳解,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Listvie簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能
這篇文章主要為大家詳細(xì)介紹了Listvie簡(jiǎn)單實(shí)現(xiàn)購(gòu)物車(chē)功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-08-08安卓圖片反復(fù)壓縮后為什么普遍會(huì)變綠而不是其它顏色?
今天小編就為大家分享一篇關(guān)于安卓圖片反復(fù)壓縮后為什么普遍會(huì)變綠而不是其它顏色?,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12Android?Navigation重建Fragment問(wèn)題分析及解決
這篇文章主要介紹了Android?Navigation重建Fragment問(wèn)題分析及解決,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09