【发布时间】:2013-04-29 18:12:20
【问题描述】:
我有一个意图选择器,可以让我从画廊或相机中选择图像,如下所示:
Intent galleryIntent = new Intent(Intent.ACTION_GET_CONTENT,null);
galleryIntent.setType("image/*");
galleryIntent.addCategory(Intent.CATEGORY_OPENABLE);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Intent chooser = new Intent(Intent.ACTION_CHOOSER);
chooser.putExtra(Intent.EXTRA_INTENT, galleryIntent);
chooser.putExtra(Intent.EXTRA_TITLE, "title");
Intent[] intentArray = {cameraIntent};
chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentArray);
startActivityForResult(chooser,REQUEST_CODE);
我希望我的onActivityResult 方法是这样的:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if(Condition == picture_coming_from_gallery)
{
//my code here
}
else if(condition == picture_coming_from_camera)
{
//another code here
}
}
什么条件可以让我知道我的图像来自哪个来源?
更新:
现在它正在工作,这是解决方案:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
if(data.getData()!=null)
{
try
{
if (bitmap != null)
{
bitmap.recycle();
}
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
感谢您的帮助!
【问题讨论】:
-
我建议您使用 GridView 来填充您的操作,并分配一个 OnItemClickListener 来捕获单击了哪个项目。
-
@ChorWaiChun - 他正在通过
Intent打开一个外部Activity,因此他无法控制图片的显示。 -
@ianhanniballake 这就是为什么他应该通过 GridView 手动填充选择器,使用 Dialog 显示,而不是“打开外部 Avtivity”
标签: android android-intent gallery android-camera-intent