2012-04-16 78 views
2

我目前正在爲其中發生了一些問題的Android開發相機應用程序。我需要它可以在所有Android設備上工作,並且由於所有這些設備都以與照相機硬件不同的方式工作,所以我很難找到適用於所有設備的解決方案。適用於所有Android設備的相機應用程序

我的應用程序的主要目標是啓動按鈕點擊相機,拍照並上傳到服務器。所以我並不需要將圖像保存在設備上的功能,但如果需要進一步使用圖像,我不妨允許它。

例如,我正在三星Galaxy SII和摩托羅拉Pad上測試我的應用程序。我得到的工作是啓動相機,這是由路C#代碼,因爲我使用MonoDroid的代碼:

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/ 爲什麼我跟着這個指南是因爲活動在我的銀河設備上返回null(另一個面向設備的問題)。

此代碼在Galaxy設備上正常工作。它拍攝一張照片並將照片保存在我可以上傳到服務器的圖庫中。通過進一步的研究,這顯然是銀河標準行爲,所以這不適用於我的摩托羅拉墊。相機工作正常,但沒有圖像保存到畫廊。

所以在這個背景下,我的問題是,我在正確的道路上嗎?我是否需要將圖像保存到圖庫以便在我的應用程序中進一步使用?是否有任何解決方案適用於每個Android設備,因爲這是我需要的解決方案。

感謝您的任何反饋!

回答

1

要知道: 是的相同的概念適用於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); 
      } 
     } 
    } 
} 
+0

我下面的代碼,但data.DataString是空的我.. – 2012-05-18 07:55:20

1

閱讀鏈接的文章後,該文章採用的方法是面向Galaxy系列,因爲它們似乎自動寫入到圖庫中。

本文討論的一些其他場景中的細節:

Android ACTION_IMAGE_CAPTURE Intent

所以,我並不認爲以下您所提供的鏈接的文章是正確的道路。並非所有設備都按照該文章afaik中的描述自動寫入圖庫。我鏈接的文章指出與安全相關的問題,並建議將圖像寫入/ sdcard/tmp文件夾以存儲原始圖像。走向類似的道路很可能會導致代碼在許多設備上可靠工作。

下面是一些供參考的其他鏈接:

關於這方面谷歌討論:http://code.google.com/p/android/issues/detail?id=1480 項目一個有潛力的解決問題的辦法:https://github.com/johnyma22/classdroid

雖然這一討論/項目是Java/Android的SDK,相同的概念應適用於Monodroid。如果您需要幫助,我很樂意幫助您將代碼調整爲適用於Android的Mono工作方案。

相關問題