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