開啟媒體庫選擇圖片
開啟媒體庫
Intent intent = new Intent();
// 開啟Pictures畫面Type設定為Image
intent.setType("image/*");
// 使用Intent.ACTION_GET_CONTENT這個Action
intent.setAction(Intent.ACTION_GET_CONTENT);
// 取得相片後返回本畫面
this.startActivityForResult(intent, 1);
回傳選擇圖片
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
uri = data.getData();
ContentResolver cr = this.getContentResolver();
try {
bitmap = BitmapFactory.decodeStream(cr.openInputStream(uri));
} catch (FileNotFoundException e) {
//找不到圖片發生錯誤訊息
}
}
super.onActivityResult(requestCode, resultCode, data);
}