2015-10-16 83 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。 –