【问题标题】:Camera application for all Android devices适用于所有 Android 设备的相机应用程序
【发布时间】:2012-04-16 10:35:46
【问题描述】:

我目前正在开发适用于 Android 的相机应用程序,但出现了一些问题。我需要它在所有 Android 设备上工作,并且由于所有这些都以不同的方式特别适用于相机硬件,我很难找到适用于所有设备的解决方案。

我的应用程序的主要目标是单击按钮启动相机,拍照并将其上传到服务器。所以我真的不需要将图像保存在设备上的功能,但如果需要进一步使用图像,我不妨允许它。

例如,我正在三星 Galaxy SII 和摩托罗拉 Pad 上测试我的应用程序。我得到了启动相机的工作代码,因为我使用的是 Monodroid,所以顺便说一下 C# 代码:

Intent cameraIntent = new Intent(Android.Provider.MediaStore.ActionImageCapture);  
StartActivityForResult(cameraIntent, PHOTO_CAPTURE);

然后我获取结果,类似于我遵循的本指南: http://kevinpotgieter.wordpress.com/2011/03/30/null-intent-passed-back-on-samsung-galaxy-tab/ 我之所以遵循本指南是因为活动在我的 Galaxy 设备上返回 null(另一个面向设备的问题)。

此代码在 Galaxy 设备上运行良好。它会拍一张照片并将照片保存在画廊中,我可以从中上传到服务器。通过进一步研究,这显然是星系标准行为,所以这在我的摩托罗拉平板电脑上不起作用。相机工作正常,但没有图像保存到图库。

因此,在这种背景下,我的问题是,我是否走在正确的道路上?我是否需要将图像保存到图库以便在我的应用程序中进一步使用?是否有任何适用于所有 Android 设备的解决方案,因为这是我需要的解决方案。

感谢您的任何反馈!

【问题讨论】:

    标签: camera xamarin.android android


    【解决方案1】:

    阅读链接的文章后,该文章中采用的方法是针对 Galaxy 线的,因为它们似乎会自动写入图库。

    本文详细讨论了其他一些场景:

    Android ACTION_IMAGE_CAPTURE Intent

    因此,我不一定认为遵循您提供的链接文章是正确的路径。并非所有设备都会按照该文章 afaik 中的描述自动写入图库。我链接到的文章指出了与安全相关的问题,并建议将图像写入 /sdcard/tmp 文件夹以存储原始图像。走类似的道路很可能会导致代码在许多设备上可靠地工作。

    这里有一些其他链接供参考:

    关于此主题的 Google 讨论:http://code.google.com/p/android/issues/detail?id=1480 有可能解决问题的项目:https://github.com/johnyma22/classdroid

    虽然该讨论/项目在 Java/Android SDK 中,但相同的概念应该适用于 Monodroid。如果您需要帮助,我很乐意帮助您将代码调整为适用于 Android 的 Mono 解决方案。

    【讨论】:

      【解决方案2】:

      到long2know: 是的,同样的概念也适用于 Monodroid。我已经阅读了您链接的堆栈文章以及其他类似文章。但是我不喜欢该特定帖子中的方法,因为它会检查某些硬编码到集合中的设备的错误。这意味着它可能无法检测到未来设备中的错误。由于我不会对此应用程序进行维护,因此我不能允许这样做。我在其他地方找到了一个解决方案并将其调整到我的情况,如果有人需要,我会在下面发布。它适用于我的两种设备,猜测它适用于大多数其他设备。谢谢你的帖子!

      允许您拍照并使用的解决方案,还可以选择使用图库中的图像。解决方案出于这些目的使用选项菜单,仅用于测试。 (Monodroid 代码)。

      相机代码的灵感来自: access to full resolution pictures from camera with MonoDroid

      namespace StackOverFlow.UsingCameraWithMonodroid
      {
          [Activity(Label = "ImageActivity")]
          public class ImageActivity
              private readonly static int TakePicture = 1;
              private readonly static int SelectPicture = 2;
              private string imageUriString;
      
              protected override void OnCreate(Bundle bundle)
              {
                  base.OnCreate(bundle);
                  this.SetContentView(Resource.Layout.ImageActivity);
              }
      
              public override bool OnCreateOptionsMenu(IMenu menu)
              {
                  MenuInflater flate = this.MenuInflater;
                  flate.Inflate(Resource.Menu.ImageMenues, menu);
      
                  return base.OnCreateOptionsMenu(menu);
              }
      
              public override bool OnOptionsItemSelected(IMenuItem item)
              {
                  switch (item.ItemId)
                  {
                      case Resource.Id.UseExisting:
                          this.SelectImageFromStorage();
                          return true;
                      case Resource.Id.AddNew:
                          this.StartCamera();
                          return true;
                      default:
                          return base.OnOptionsItemSelected(item);
                  }
              }
      
              private Boolean isMounted
              {
                  get
                  {
                      return Android.OS.Environment.ExternalStorageState.Equals(Android.OS.Environment.MediaMounted);
                  }
              }
      
              private void StartCamera()
              {
                  var imageUri = ContentResolver.Insert(isMounted ? MediaStore.Images.Media.ExternalContentUri
                                          : MediaStore.Images.Media.InternalContentUri, new ContentValues());
      
                  this.imageUriString = imageUri.ToString();
      
                  var cameraIntent = new Intent(MediaStore.ActionImageCapture);
                  cameraIntent.PutExtra(MediaStore.ExtraOutput, imageUri);
                  this.StartActivityForResult(cameraIntent, TakePicture);
              }
      
              private void SelectImageFromStorage()
              {
                  Intent intent = new Intent();
                  intent.SetType("image/*");
                  intent.SetAction(Intent.ActionGetContent);
                  this.StartActivityForResult(Intent.CreateChooser(intent,
                          "Select Picture"), SelectPicture);
      
              }
      
              // Example code of using the result, in my case i want to upload in another activity
              protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
              {
                  // If a picture was taken
                  if (resultCode == Result.Ok && requestCode == TakePicture)
                  {
                      // For some devices data can become null when using the camera activity.
                      // For this reason we save pass the already saved imageUriString to the upload activity
                      // in order to adapt to every device. Instead we would want to use the data intent
                      // like in the SelectPicture option.
      
                      var uploadIntent = new Intent(this.BaseContext, typeof(UploadActivity));
                      uploadIntent.PutExtra("ImageUri", this.imageUriString);
                      this.StartActivity(uploadIntent);
                  }
                  // User has selected a image from storage
                  else if (requestCode == SelectPicture)
                  {
                      var uploadIntent = new Intent(this.BaseContext, typeof(UploadActivity));
                      uploadIntent.PutExtra("ImageUri", data.DataString);
                      this.StartActivity(uploadIntent);
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-01
        • 1970-01-01
        • 1970-01-01
        • 2018-08-10
        • 2014-04-17
        • 2022-07-22
        • 1970-01-01
        相关资源
        最近更新 更多