在活动之间传递图像

时间:2017-03-14 18:58:04

标签: c# android android-intent xamarin.android

我按钮时制作照片

我也在布局中显示这张照片。

我需要在另一个活动中显示它

这是代码

 protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);
        Intent mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
        Uri contentUri = Uri.FromFile(App._file);
        mediaScanIntent.SetData(contentUri);
        SendBroadcast(mediaScanIntent);
        int height = Resources.DisplayMetrics.HeightPixels;
        int width = _imageView.Height;
        App.bitmap = App._file.Path.LoadAndResizeBitmap(width, height);
        if (App.bitmap != null)
        {
            _imageView.SetImageBitmap(App.bitmap);
            App.bitmap = null;
        }

        // Dispose of the Java side bitmap.
        GC.Collect();
    }

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        surname_from_activity = Intent.GetStringExtra("Surname");
        inn_from_activity = Intent.GetStringExtra("INN");
        System.Console.WriteLine(surname_from_activity);
        SetContentView(Resource.Layout.qrandphotolayout);
        ImageView qr = FindViewById<ImageView>(Resource.Id.qrimage);

        if (IsThereAnAppToTakePictures())
         {
            CreateDirectoryForPictures();
            Button button = FindViewById<Button>(Resource.Id.photobutton);
            _imageView = FindViewById<ImageView>(Resource.Id.photo);
            button.Click += TakeAPicture;
         }

        Button next = FindViewById<Button>(Resource.Id.sync);
        next.Click += delegate {
            var intent = new Intent(this, typeof(Badge));
            intent.PutExtra("Surname", surname_from_activity);
            intent.PutExtra("INN", inn_from_activity);
            StartActivity(intent);
        };
    }

   private void CreateDirectoryForPictures()
         {
             App._dir = new File(
                 Environment.GetExternalStoragePublicDirectory(
                     Environment.DirectoryPictures), "CameraAppDemo");
             if (!App._dir.Exists())
             {
                 App._dir.Mkdirs();
             }
         }

         private bool IsThereAnAppToTakePictures()
         {
             Intent intent = new Intent(MediaStore.ActionImageCapture);
             IList<ResolveInfo> availableActivities =
                 PackageManager.QueryIntentActivities(intent, PackageInfoFlags.MatchDefaultOnly);
             return availableActivities != null && availableActivities.Count > 0;
         }

         private void TakeAPicture(object sender, EventArgs eventArgs)
         {
             Intent intent = new Intent(MediaStore.ActionImageCapture);
             App._file = new File(App._dir, String.Format(surname_from_activity + inn_from_activity, Guid.NewGuid()));
             intent.PutExtra(MediaStore.ExtraOutput, Uri.FromFile(App._file));
             StartActivityForResult(intent,0);
         }
    }

我需要将图像传递给另一个活动并在ImageView中显示它

我怎么能这样做?

非常感谢

2 个答案:

答案 0 :(得分:1)

<强>可能性

  • 您可以在ActivityB中创建静态属性,在从ActivityA调用ActivityB之前,您应该将imageView / URI传递给创建的属性。

  • 您可以在文件夹temp(ActivityB)中传递保存图像,并在ActivityB中获取此图像。

您使用intent / bundle传递图像是否太重

答案 1 :(得分:1)

发件人活动

Intent intent = new Intent(this, TwoActivity.class);
Bitmap bitmap; 
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 50, bs);
i.putExtra("byte", bs.toByteArray());
startActivity(i);

接收者活动

if(getIntent().hasExtra("byte")) {
ImageView imv= new ImageView(this);
Bitmap bitmap = BitmapFactory.decodeByteArray(
        getIntent().getByteArrayExtra("byte"),0,getIntent().getByteArrayExtra("byte").length);        
imv.setImageBitmap(bitmap);
}