Nougat getUriForFile问题与相机返回

时间:2017-02-23 15:25:20

标签: java android android-7.0-nougat

我一直在尝试将我的应用更新为Nougat在意图中处理URI的方式,并且不能为我的生活找出如何让相机再次工作。我已经尝试过遵循文档但我必须遗漏一些东西。有人可以帮忙吗?

这是我的派遣事件

Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File
            return;
        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                   "com.everywhere_ww.provider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
        }
    }

这是我在清单中的提供者:

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

这是我的档案路径

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="imgFolder" path="pictures" />
<external-path name="tempFolder" path="temp" />

尝试拍照总是失败

java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.everywhere_ww/files/temp/JPEG_20170223_101548_1550424251.jpg

2 个答案:

答案 0 :(得分:0)

/data/data/com.everywhere_ww/files/temp/JPEG_20170223_101548_1550424251.jpg似乎是指向internal storage的路径。您可能是通过getFilesDir()创建的。

替换:

<external-path name="tempFolder" path="temp" />

使用:

<files-path name="tempFolder" path="temp" />

答案 1 :(得分:0)

嗨,不确定可以有所不同,但尝试将以下参数添加到您的xml文件路径

var xhr = new XMLHttpRequest();
console.log("Sending the request...");
xhr.open("POST", urlBase + "QueryResults", true);
xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
xhr.onreadystatechange = function () {
  if (xhr.readyState == 4) {
    console.log("return: ", xhr.responseText); //data is logged properly
    return xhr.responseText; //it should be returned properly
  }
};
xhr.send(JSON.stringify(queryRequest));