Retrieving Content Uri using File Provider fails

牧云@^-^@ 提交于 2021-01-28 14:13:27

问题


I have spent days reading the docs and following various tutorials for this but I am getting errors whatever I do. I am trying to save an image in Internal Storage and then convert it to a content Uri so that I can use the system crop and resize function. If anyone could spot where my code is wrong I would be very grateful:

Main Activity:

new Thread(() -> {
try {
    String temp = "temp";

    //Convert bitmap to byte array
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bos);
    byte[] bitmapData = bos.toByteArray();

    //Write the bytes in file
    FileOutputStream fos = openFileOutput(temp, MODE_PRIVATE);
    fos.write(bitmapData);
    fos.flush();
    fos.close();

    //Convert file to contentUri
    File file = new File(getFilesDir(), temp);

    Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.setFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    Uri imageUri = FileProvider.getUriForFile(MainActivity.this, "@string/file_provider_authority", file);
    intent.setDataAndType(imageUri, "image/jpg");

    intent = WallpaperManager.getInstance(getApplicationContext()).getCropAndSetWallpaperIntent(imageUri);
    startActivityForResult(intent, ACTIVITY_CROP);

} catch (Exception e) {
    e.printStackTrace();
}
}).start();

Manifest:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="@string/file_provider_authority"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_provider_paths" />
    </provider>
</application>

file_provider_paths.xml:

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

strings.xml:

<resources>
<string name="file_provider_authority"
    translatable="false">com.me.wallpaper_app.provider
</string>

Error:

    W/System.err(12162): java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
    W/System.err(12162):    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:604)
    W/System.err(12162):    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:578)
    W/System.err(12162):    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:416)
    W/System.err(12162):    at com.me.wallpaper_app.MainActivity$1$1.lambda$onResourceReady$0$MainActivity$1$1(MainActivity.java:82)
    W/System.err(12162):    at com.me.wallpaper_app.-$$Lambda$MainActivity$1$1$Xm-7yym42zTgTZOFP5GGtbF6p2U.run(Unknown Source:4)
    W/System.err(12162):    at java.lang.Thread.run(Thread.java:919)

回答1:


"@string/file_provider_authority" is not how you reference string resources in Java. Use getString(R.string.file_provider_authority).

UPDATE: Based on the comments, the second major problem is that the consumer of this content isn't using the MIME type from the Intent, but instead is calling getType() on a ContentResolver to get the type from the ContentProvider. In this case, that is your FileProvider, and FileProvider only knows how to work with file extensions and converting those to MIME types. So, using temp.jpeg gave FileProvider the information necessary to return the proper MIME type.




回答2:


Try This

      Uri imageUri = FileProvider.getUriForFile(MainActivity.this, R.string.file_provider_authority, file);

Also Change

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

To

    <?xml version="1.0" encoding="utf-8"?>
    <paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-path name="external_files" path="."/>
    </paths>


来源:https://stackoverflow.com/questions/56306322/retrieving-content-uri-using-file-provider-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!