Android 图像裁剪 Uri 异常

标签 android android-intent xamarin onactivityresult

首先,我使用的是 Xamarin,但问题在原生 Java 项目上是一样的。 我正在将 SDK 更新到 5.1,并且在之前运行良好的代码上遇到了一个奇怪的错误。

  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,然后在调用 Intent 之前检索它?还是我错过了一个明显的解决方案?

干杯 汤姆

最佳答案

好吧,看来我真的错过了一些东西,扩展了 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);

我觉得这可能有点矫枉过正,如果有的话请随时发表评论。但至少它解决了问题。

关于Android 图像裁剪 Uri 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33166216/

相关文章:

单个 Activity 中的android多 View 寻呼机不显示第二个 View 寻呼机

不支持 google api 的手机的 Android 应用程序崩溃

c# - (x86)\MSBuild\Xamarin\Android\Xamarin.Android.Common.targets(2053,3) : error MSB6006: "java.exe" exited with code 1

java - Android JSON 解析问题

android - 找不到符号类 AutoValueGson_AutoValueGsonFactory

android - 在应用程序终止或终止期间调用 android 应用程序中的方法

android - 在广播接收器中检测呼出调用结束状态

java - 如何在 Intent 中传递 Parcelable?

azure - 用于 Azure AD B2C 和 Xamarin 的 MSAL 或 ADAL 库

java - 为什么我的 Android 应用程序会定期崩溃?