xamarin 如何从一个Activity获取图像视图并将其设置为另一个Activity的背景?

c2e8gylq  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(523)

我的Android应用程序中有两个页面,我想将第一个页面的背景设置为第二个页面上Image View中显示的图像。我如何以编程方式实现这一点?我一直在尝试将Android应用程序中第二页的背景设置为第一页Image View中显示的图像。我已经尝试实现这一点,但它似乎并不像预期的那样工作。有人能检查我的代码并提供如何正确操作的指导吗?
第一活动,从该第一活动获取图像视图

using Android.App;
using Android.Content;
using Android.OS;
using Android.Provider;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Android.Graphics;

using SQLite;

namespace HAMANIA_APP
{
    [Activity(Label = "GallerySaver")]
    public class GallerySaver : Activity
    {
        Button camera, save, uploadPhoto;
        ImageView pic;
        Bitmap bitmap;
        bool mExternalStorageAvailable = false;
        bool mExternalStorageWriteable = false;
        public static readonly int imageid = 1000;

        

        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Phone_Gallery_Saver);
            camera = FindViewById<Button>(Resource.Id.btnCamera);
            save = FindViewById<Button>(Resource.Id.btnSave);
            pic = FindViewById<ImageView>(Resource.Id.gallery);
            save.Click += Save_Click;
            camera.Click += Camera_Click;
            

            // Create your application here
        }

        

        private void Save_Click(object sender, EventArgs e)
        {
            Intent inte = new Intent();
            inte.SetType("image/*");
            inte.SetAction(Intent.ActionGetContent);
            StartActivityForResult(Intent.CreateChooser(inte, "Select Picture"), imageid);

            Toast.MakeText(Application.Context, "Image saved to gallery", ToastLength.Long).Show();
        }

        private void Camera_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(MediaStore.ActionImageCapture);
            StartActivityForResult(intent, 0);
        }
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (requestCode == 0)//coming from camera
            {
                if (resultCode == Result.Ok)
                {
                    Bundle extras = data.Extras;
                    bitmap = (Bitmap)extras.Get("data");
                    pic.SetImageBitmap(bitmap);
                    //bitmap = (Android.Graphics.Bitmap)data.Extras.Get("data");
                    // pic.SetImageBitmap(bitmap);
                    
                }
                
                }else
                {
                    if ((requestCode == imageid) && (resultCode == Result.Ok) && (data != null))
                    {
                        Android.Net.Uri uri = data.Data;
                        bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, uri);
                        pic.SetImageBitmap(bitmap);
                    var image = (Bitmap)data.Extras.Get("data");

                    

                }
            }
        }

        
    }
}

第二页,用于设置背景

using Android.App;
using Android.Content;
using Android.Graphics;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Essentials;
using Android.Provider;
using Java.IO;
using System.IO;
using Android.Net;
using AndroidX.Core.Content;
using Android.Graphics.Drawables;

namespace HAMANIA_APP
{
    [Activity(Label = "MenuActivity")]
    public class MenuActivity : Activity
    {
        Button camera, gallery, control;
        BroadcastBattery broadcastBattery;
        TextView tv;
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            SetContentView(Resource.Layout.Menu);
            camera = FindViewById<Button>(Resource.Id.BLUETOOTH);
            tv = FindViewById<TextView>(Resource.Id.tv);
            broadcastBattery = new BroadcastBattery(tv);
            control = FindViewById<Button>(Resource.Id.control);
            control.Click += Control_Click;
            gallery = FindViewById<Button>(Resource.Id.gallery);
            camera.Click += Camera_Click;
           
        }

        public static string BackgroundImagePath { get; set; }

        private void Control_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(this, typeof(ControlCenter));
            StartActivity(intent);
        }

        private void Camera_Click(object sender, EventArgs e)
        {
            Intent intent2 = new Intent(this, typeof(GallerySaver));
            StartActivity(intent2);
        }

        
    }
}

我很感激你的帮助

jm2pwxwz

jm2pwxwz1#

您可以尝试将位图转换为字节数组,并通过Intent将其传递回MenuActivity
根据你的代码,我创建了一个演示,并实现了这个功能。它在我这边工作正常。
可以参考以下代码:

MenuActivity.cs

为了从活动GallerySaver接收数据,我将代码StartActivity(intent2);替换为StartActivityForResult方法Camera_Click

Intent intent2 = new Intent(this, typeof(GallerySaver));
  //StartActivity(intent2);

  StartActivityForResult (intent2, 0);

并覆盖方法OnActivityResult
请参考以下代码:

public class MenuActivity : Activity
{
    Button camera, gallery, control;
    //BroadcastBattery broadcastBattery;
    TextView tv;

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

        // Create your application here
        SetContentView(Resource.Layout.Menu);

        camera = FindViewById<Button>(Resource.Id.BLUETOOTH);
        tv = FindViewById<TextView>(Resource.Id.tv);
        //broadcastBattery = new BroadcastBattery(tv);
        //control = FindViewById<Button>(Resource.Id.control);
        //control.Click += Control_Click;
        gallery = FindViewById<Button>(Resource.Id.gallery);
        camera.Click += Camera_Click;
         
       //rootlayout is the root layout of this activity
        rootlayout = FindViewById<LinearLayout>(Resource.Id.rootlayout);    
    }

    private void Camera_Click(object sender, EventArgs e)
    {
        Intent intent2 = new Intent(this, typeof(GallerySaver));
        //StartActivity(intent2);

        StartActivityForResult (intent2, 0);
    }

    protected override void OnActivityResult(int requestCode, [GeneratedEnum] Result resultCode, Intent data)
    {
        base.OnActivityResult(requestCode, resultCode, data);

        if (resultCode == Result.Ok)
        {
            // Get image from intent as ByteArray
            var image = data.GetByteArrayExtra("picture");
            if (image != null)
            {
                //Convert byte array back into bitmap
                Bitmap bitmap = BitmapFactory.DecodeByteArray(image, 0, image.Length);

                BitmapDrawable drawable = new BitmapDrawable(bitmap);

                 // set Background for this activity
                rootlayout.Background = drawable;
            }
        }
    }

}

GallerySaver.cs

捕获图片后,您可以将位图转换为字节数组并通过Intent传递回来,并调用SetResult

//convert bitmap into byte array
                    byte[] bitmapData;
                    using (var stream = new MemoryStream())
                    {
                        bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
                        bitmapData = stream.ToArray();
                    }

                    Intent intent = new Intent();
                    intent.PutExtra("picture", bitmapData);

                    SetResult(Result.Ok, intent);
                    Finish();

GallerySaver.cs的完整代码:

[Activity(Label = "GallerySaver")] 
    public class GallerySaver : Activity
    {
        Button camera, save, uploadPhoto;
        ImageView pic;
        Bitmap bitmap;
        public static readonly int imageid = 1000;

        Intent intent;

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

            // Create your application here
            SetContentView(Resource.Layout.Phone_Gallery_Saver);

            camera = FindViewById<Button>(Resource.Id.btnCamera);
            save = FindViewById<Button>(Resource.Id.btnSave);
            pic = FindViewById<ImageView>(Resource.Id.gallery);
            save.Click += Save_Click;
            camera.Click += Camera_Click;
        }

        private void Save_Click(object sender, EventArgs e)
        {
            Intent inte = new Intent();
            inte.SetType("image/*");
            inte.SetAction(Intent.ActionGetContent);
            StartActivityForResult(Intent.CreateChooser(inte, "Select Picture"), imageid);

            Toast.MakeText(Application.Context, "Image saved to gallery", ToastLength.Long).Show();
        }

        private void Camera_Click(object sender, EventArgs e)
        {
            Intent intent = new Intent(MediaStore.ActionImageCapture);
            StartActivityForResult(intent, 0);
        }

        [Obsolete]
        protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
        {
            base.OnActivityResult(requestCode, resultCode, data);
            if (requestCode == 0)//coming from camera
            {
                if (resultCode == Result.Ok)
                {
                    Bundle extras = data.Extras;
                    bitmap = (Bitmap)extras.Get("data");
                    pic.SetImageBitmap(bitmap);

                    //convert bitmap into byte array
                    byte[] bitmapData;
                    using (var stream = new MemoryStream())
                    {
                        bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream);
                        bitmapData = stream.ToArray();
                    }

                    Intent intent = new Intent();
                    intent.PutExtra("picture", bitmapData);

                    SetResult(Result.Ok, intent);
                    Finish();

                }

            }
            else
            {
                if ((requestCode == imageid) && (resultCode == Result.Ok) && (data != null))
                {
                    Android.Net.Uri uri = data.Data;
                    bitmap = MediaStore.Images.Media.GetBitmap(ContentResolver, uri);
                    pic.SetImageBitmap(bitmap);
                    var image = (Bitmap)data.Extras.Get("data");

                    // you also use SetResult to achieve this.
                }
            }
        }

    }

相关问题