【问题标题】:How to get URI for file correctly in Android?如何在 Android 中正确获取文件的 URI?
【发布时间】:2017-01-13 17:48:38
【问题描述】:

我正在尝试获取这样的文件的 URI:

Uri photoURI = FileProvider.getUriForFile(this,
                        "br.com.[blablabla].sgaconsultor",
                        createImageFile());

private File createImageFile() throws IOException {
        // Create an image file name
        String imageFileName = "registroFoto";

        ContextWrapper cw = new ContextWrapper(getApplicationContext());
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);

        return File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                directory
        );
    }

我有一个文件提供者:

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

xml 是:

<paths>
    <files-path name="br.com.[blablabla].blabla" path="app_imageDir" />
</paths>

但是当我运行这段代码时,我总是得到这个错误:

java.lang.IllegalArgumentException:Android: FileProvider IllegalArgumentException Failed to find configured root that contains /data/data/**/files/Videos/final.mp4

但到目前为止还没有运气...... 那么我该如何解决这个问题并获取我的文件的 URI?

非常感谢任何帮助!

【问题讨论】:

    标签: java android image storage


    【解决方案1】:

    那么我该如何解决这个问题并获取我的文件的 URI?

    第 1 步:去除 Java 代码中无用的东西。

    步骤 #2:获取 Java 代码和 file_paths 资源以进行匹配,因为 file_paths 表示文件路径必须在 getFilesDir() 下并且其中包含 app_imageDir,而您的 Java 代码没有有这两样东西。

    那么,试试这个:

    private File createImageFile() throws IOException {
        // Create an image file name
        String imageFileName = "registroFoto";
        File directory = new File(getFilesDir(), "app_imageDir");
    
        directory.mkdirs();
    
        return File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                directory
        );
    }
    

    【讨论】:

      猜你喜欢
      • 2018-12-14
      • 1970-01-01
      • 1970-01-01
      • 2019-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      相关资源
      最近更新 更多