FileProvider.GetUriForFile()在Xamarin Android中出现RuntimeException

时间:2016-11-12 10:34:36

标签: android xamarin xamarin.android

我要从Xamarin Android中的Camera拍照。

这是我在Xamarin所做的事情。

  1. 启动相机活动

    private void CallTakePictureIntent ()
    {
        Intent takePictureIntent = new Intent (MediaStore.ActionImageCapture);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.ResolveActivity (PackageManager) != null) {
            // Create the File where the photo should go
            Java.IO.File photoFile = null;
            try {
                photoFile = ImageHelper.CreateImageFile (this);
            } catch (Exception ex) {
                System.Console.WriteLine (ex.ToString ());
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                sURI photoURI = FileProvider.GetUriForFile (this, UIHelper.FileProvider, photoFile);
                requestedAvatarUri = photoURI;
                takePictureIntent.PutExtra (MediaStore.ExtraOutput, photoURI);
                StartActivityForResult (takePictureIntent, REQUEST_TAKE_PICTURE);
            }
        }
    }
    
  2. 创建临时文件。

    public static Java.IO.File CreateImageFile (Context context)
    {
        // Create an image file name
        string timeStamp = DateTime.Now.ToString ("yyyyMMdd_HHmmss");
        string imageFileName = "JPEG_" + timeStamp + "_";
        Java.IO.File storageDir = context.GetExternalFilesDir (Android.OS.Environment.DirectoryPictures);
        Java.IO.File image = Java.IO.File.CreateTempFile (
                imageFileName,  /* p                refix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
        return image;
    }
    
  3. Android.Manifest

        <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.masterbee.xamapp-dev.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>     
    
  4. XML / file_paths.xml

    <external-path name="external_images" path="Pictures/" />
    
  5. 我认为我已经正确完成了所有工作,除了FileProvider.GetUriForFile()行中的运行时异常。

    这里的通话追踪。 enter image description here

0 个答案:

没有答案