2015-10-16 42 views
3

首先,我使用Xamarin,但問題與原生Java項目相同。 我正在更新SDK到5.1,並遇到一個奇怪的錯誤,以前工作正常的代碼。Android Image裁剪Uri異常

imageStream = "file://" + imageStream; 

      Mvx.Trace("path: " + imageStream); 

      img = imageStream; 

      try { 
       Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
       // indicate image type and Uri 
       var fileUri = Android.Net.Uri.Parse(imageStream); 
       cropIntent.SetDataAndType(fileUri, "image/*"); 
       // set crop properties 
       cropIntent.PutExtra("crop", "true"); 

       // indicate aspect of desired crop 
       cropIntent.PutExtra("aspectX", 5); 
       cropIntent.PutExtra("aspectY", 4); 
       // indicate output X and Y 
       cropIntent.PutExtra("outputX", 1000); 
       cropIntent.PutExtra("outputY", 800); 
       // retrieve data on return 
       cropIntent.PutExtra("return-data", true); 

       // start the activity - we handle returning in onActivityResult 
       StartActivityForResult(cropIntent, PIC_CROP); 
      } 

imageStream是文件的路徑。圖像裁剪本身加載正常,並工作。然而,當我點擊保存,我得到以下異常中的logcat:

E/AndroidRuntime(5333): FATAL EXCEPTION: BackgroundTask #1 
E/AndroidRuntime(5333): Process: com.google.android.apps.photos, PID: 5333 
E/AndroidRuntime(5333): java.lang.IllegalArgumentException: mediaStoreUri must be a MediaStore Uri 

我沒有找到在MediaStore或MediaStore.Image命名空間類似的方法將Android.Net.Uri.Parse。這是否意味着我首先必須將圖像保存到MediaStore,然後在調用意圖之前檢索它?還是有一個明顯的解決方案,我只是錯過了?

乾杯 湯姆

回答

3

好吧,似乎我真的錯過了一些東西在這裏,引申爲URI檢索的代碼把圖像轉換成媒體商店第一。

var file = new Java.IO.File(imageStream); 
var bmp = BitmapFactory.DecodeFile(file.AbsolutePath); 
img = MediaStore.Images.Media.InsertImage(ContentResolver, bmp, "" ,""); 
var fileUri = Android.Net.Uri.Parse(img); 

我覺得這可能是矯枉過正,如果可以隨意發表評論。但至少它解決了這個問題。

+0

我在Android 6上有同樣的例外。MediaStore.Images.Media.InsertImage修復了我的裁剪!謝謝你的文章! 雖然這很奇怪......似乎是M中的一個bug。 –