如何使用Xamarin Android中的Intents将图像从一项活动传递到另一项活动?
问题描述:
我是一名Web开发人员,但最近开始使用Xamarin探索Android开发世界,但我正在努力寻找一种方法来完成这项任务。如何使用Xamarin Android中的Intents将图像从一项活动传递到另一项活动?
我的图像位于drawables-hdpi。
在我的主要活动,我上的ImageView使用本教程http://developer.xamarin.com/recipes/android/resources/general/load_large_bitmaps_efficiently/
现在设置一个标题图片,我创建了另一个活动时,在那里我的头图像的用户点击,第二个活动进入行动,允许用户平移和缩放图像。
我需要第二个活动来动态地从第一个活动接收图像。
这是我尝试过但没有成功。我在第二项活动中也使用了相同的“高效载入图像”代码,但我不认为这很重要。
// set image
BitmapFactory.Options options = await GetBitmapOptionsOfImage();
Bitmap bitmapToDisplay = await LoadScaledDownBitmapForDisplayAsync (Resources, options, 400, 400);
headerImage.SetImageBitmap(bitmapToDisplay);
headerImage.Click += (object sender, EventArgs e) => {
var intent = new Intent(this, typeof(ImageScaleActivity));
headerImage.BuildDrawingCache();
Bitmap image = headerImage.GetDrawingCache(true);
Bundle extras = new Bundle();
extras.PutParcelable("imagebitmap", image);
intent.PutExtras(extras);
// TODO: dynamically get image name and send to next activity
StartActivity(intent);
};
该代码不工作,我没有得到任何错误,但是当我我的头图像上挖掘,什么也不显示在第二个活动,所以图像显然是不被发送。
下面是我的第二个活动中的代码。
// set image
BitmapFactory.Options options = await GetBitmapOptionsOfImage();
Bitmap bitmapToDisplay = await LoadScaledDownBitmapForDisplayAsync (Resources, options, 400, 400);
//expandedImage.SetImageBitmap(bitmapToDisplay);
Bundle extras = Intent.Extras;
Bitmap bmp = (Bitmap) extras.GetParcelable("imagebitmap");
expandedImage.SetImageBitmap(bmp);
我只是不认为我会这样做的正确方法,我不明白为什么这样的事情似乎很难做到!
答
如果您的绘图中有图像,则无需将其发送到第二个活动。把它设置成你的第二个活动就像这样。
ImageView imgView=(ImageView) findViewById(R.id.imgView);
Drawable drawable = getResources().getDrawable(R.drawable.img);
imgView.setImageDrawable(drawable);
答
You need to put the image in the Intent referring to the Activity 2.
You can use byte array to send the Image.
Byte[] pic; // Contains the Picture.
In you Main Activity make an intent of Activity2.
Intent myIntent = new Intent(this, typeof(Activity2));
myIntent.PutExtra("Picture_from_Activity1",pic);
this.StartActivity(myIntent);
Now in Activity2 Receive and display the picture.
byte[] recPic = Intent.GetByteArrayExtra("Picture_from_Activity1");
Now convert this Picture to bitmap and display it.
Bitmap bitmap = BitmapFactory.DecodeByteArray(recPic,0,recPic.Length);
ImageView iv;
iv.SetImageBitmap(bitmap);
:)
是他们在绘图资源,但图像将因此如果一个页面的图像数据库(绑在CMS)改变使用数据库加载那么你的做法会不会工作。 – MikeOscarEcho
如果它们在数据库中,则使用intent发送数据库中的图像url –