Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在活动之间传递图像_C#_Android_Android Intent_Xamarin.android - Fatal编程技术网

C# 在活动之间传递图像

C# 在活动之间传递图像,c#,android,android-intent,xamarin.android,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(

当我按按钮拍照时,我有活动

我也在布局中展示了这张照片

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

这是代码

 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);
         }
    }
activityresult上的受保护覆盖无效(int-requestCode、Result-resultCode、Intent数据)
{
base.OnActivityResult(请求代码、结果代码、数据);
Intent mediaScanIntent=新的Intent(Intent.ActionMediaScannerScanFile);
Uri contentUri=Uri.FromFile(App.\u文件);
mediaScanIntent.SetData(contentUri);
SendBroadcast(mediaScanIntent);
int height=Resources.DisplayMetrics.HeightPixels;
int width=_imageView.Height;
App.bitmap=App.\u file.Path.LoadAndResizeBitmap(宽度、高度);
如果(App.bitmap!=null)
{
_设置图像位图(App.bitmap);
App.bitmap=null;
}
//处理Java端位图。
GC.Collect();
}
创建时受保护的覆盖无效(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
姓氏来自于活动=Intent.GetStringExtra(“姓氏”);
inn_from_activity=Intent.GetStringExtra(“inn”);
System.Console.WriteLine(来自_活动的姓_);
SetContentView(Resource.Layout.qrandphotolayout);
ImageView qr=FindViewById(Resource.Id.qrimage);
如果(IsThereAnaptoTakePictures())
{
CreateDirectoryForPictures();
Button Button=FindViewById(Resource.Id.photobutton);
_imageView=findviewbyd(Resource.Id.photo);
按钮。单击+=TakeAPicture;
}
Button next=FindViewById(Resource.Id.sync);
下一步。单击+=委派{
var意向=新意向(此,类型(徽章));
意图。PutExtra(“姓氏”,来自活动的姓氏);
意向。PutExtra(“客栈”,客栈活动);
星触觉(意向);
};
}
私有void CreateDirectoryForPictures()
{
App.\u dir=新文件(
Environment.GetExternalStoragePublicDirectory(
环境(DirectoryPictures),“CameraAppDemo”);
如果(!App.\u dir.Exists())
{
App._dir.Mkdirs();
}
}
私人书房在这里拍摄照片()
{
意向意向=新意向(MediaStore.ActionImageCapture);
IList可用性=
PackageManager.QueryInputActivities(intent,PackageInfoFlags.MatchDefaultOnly);
return availableActivities!=null&&availableActivities.Count>0;
}
私有void TakeAPicture(对象发送方,EventArgs EventArgs)
{
意向意向=新意向(MediaStore.ActionImageCapture);
App._file=新文件(App._dir,String.Format(来自_activity的姓_+来自_activity的inn_,Guid.NewGuid());
intent.PutExtra(MediaStore.ExtraOutput,Uri.FromFile(App._文件));
StartActivityForResult(意向,0);
}
}
我需要将图像传递给另一个活动并在ImageView中显示它

我怎么能做到


非常感谢您

各种可能性

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

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

您通过意向/捆绑传递图像,因为它太重

发送者活动

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);
}
接收器活动

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);
}

无需将图像从一个活动传递到另一个活动,而是在活动之间传递图像的路径尝试在第二个活动
photo.SetImageBitmap(qrandcameraactivity.App.bitmap)中写入该路径无效