簡體   English   中英

如何使用Xamarin Android中的Intent將圖片從一個活動傳遞到另一個活動?

[英]How to pass an image from one activity to another using Intents in Xamarin Android?

我是一名Web開發人員,但最近開始使用Xamarin探索Android開發領域,但我一直在努力尋找完成此任務的方法。

我的圖像位於drawables-hdpi中。

在我的主要活動中,我已使用本教程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);

:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM