如何在程式中呼叫相本來選取相片
這次要介紹一下如何從程式中呼叫相本來選取相片,並回傳到程式中中來使用。
呼叫相本選取照片,記得還是需要自行定義回傳碼 SELECT_IMAGE_ACTIVITY_REQUEST_CODE
Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
intent.setType("image/*");
startActivityForResult(intent, SELECT_IMAGE_ACTIVITY_REQUEST_CODE);
處理回傳的相片,在onActivityResult中,先判斷回傳碼正確,再如下處理:
if (resultCode == RESULT_OK) {
Cursor cursor = null;
try {
cursor = getContentResolver().query(data.getData(), null, null, null, null);
if (cursor.moveToFirst()) {
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
String absoluteFilePath = cursor.getString(idx);
if (absoluteFilePath != null)
photoPath = absoluteFilePath;
}
} finally {
if (cursor != null) {
cursor.close();
}
}
}
http://www.patrickintw.com/blog/invoke_album_to_select_photo
全站熱搜
留言列表