如何将图像从一个活动发送到从 android Gallery 中选择的另一个活动?
Posted
技术标签:
【中文标题】如何将图像从一个活动发送到从 android Gallery 中选择的另一个活动?【英文标题】:How to send image from one activity to another activity that is selected from the android Gallery? 【发布时间】:2011-12-21 23:24:34 【问题描述】:我想设置要从 android 图库中选择的图像。 我使用此代码来获取选定的图像。
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);//
//startActivity(intent);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),10);
而onActivityResult方法是这样的:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 10 && resultCode == Activity.RESULT_OK)
Uri contentUri = data.getData();
String[] proj = MediaStore.Images.Media.DATA ;
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String tmppath = cursor.getString(column_index);
Bitmap croppedImage = BitmapFactory.decodeFile(tmppath);
go.setVisibility(View.VISIBLE);
//canvas.drawBitmap(kangoo, 130, 100, null);
//previewImage.setVisibility(View.VISIBLE);
imageSrc.setImageBitmap(croppedImage); //set to your imageview
现在,我想从图库中选择图像并将其发送到另一个活动。那么上面的代码怎么可能? 谢谢。
【问题讨论】:
【参考方案1】:您可以将图像的URI
传递给下一个Activity
。
你从onActivityResult()
得到的URI
在onCreate()
的下一个Activity
。
再次解码Bitmap
并将其设置为ImageView
【讨论】:
【参考方案2】:将图像从一个活动传递到另一个活动过于昂贵,而不是您可以将其图像路径作为字符串传递并加载。
见this 帖子
【讨论】:
【参考方案3】:在 OnActivityResult 中
Intent intent = new Intent(Activity1.this,Activity2.class);
intent.putExtra("bmp",croppedImage);
startActivity(intent);
第二个活动
Bitmap bmp = this.getIntent().getParcelableExtra("bmp");
【讨论】:
以上是关于如何将图像从一个活动发送到从 android Gallery 中选择的另一个活动?的主要内容,如果未能解决你的问题,请参考以下文章