2
我有以下代碼,列出了手機畫廊中的所有jpeg。它工作正常,但是當我選擇一張圖片時,我想用選定的圖像數據激發圖像處理活動的意圖。有任何想法嗎?如何在圖庫視圖中從選定圖像獲取圖像數據?
預先感謝馬特
Button useGallery = (Button)findViewById(R.id.loadfromgallery);
useGallery.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}}) ;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap yourSelectedImage = BitmapFactory.decodeStream(imageStream);
if(yourSelectedImage != null){
Log.e(TAG,"pic ok");
}else{
Log.e(TAG,"pic not ok");
}
}
}
}
您處理這種情況的方式取決於您傳遞數據的應用程序。如果應用程序需要位圖的URI,則必須將其作爲附加內容傳遞給您的意圖。否則,你可以傳遞一個位圖(搜索如何做),但是,我不會建議你這樣做,因爲你發送的數據很多。 – dymmeh