欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android圖像繪制(六)獲取本地圖片或拍照?qǐng)D片等圖片資源

 更新時(shí)間:2013年01月17日 10:34:33   作者:  
從SD卡中獲取圖片資源,或者拍一張新的圖片,然后再進(jìn)行處理(直接處理返回圖片/獲得圖片的地址再處理)接下來(lái)為您詳細(xì)介紹,感興趣的朋友可以了解下
從SD卡中獲取圖片資源,或者拍一張新的圖片。
先貼代碼
獲取圖片
注釋:拍照獲取的話(huà),可以指定圖片的保存地址,在此不說(shuō)明。
復(fù)制代碼 代碼如下:

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()。
復(fù)制代碼 代碼如下:

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();
}
}

處理圖片,方法二,獲得圖片的地址再處理
復(fù)制代碼 代碼如下:

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();
}
}

相關(guān)文章

最新評(píng)論