1

I have a button that should open a image file on the device. It works fine below API 24 but crashes on API 24 and above. I followed that tutorial for API 24+ but it still crashes and I don't know why.

Intent intent = new Intent(Intent.ACTION_VIEW);
if(android.os.Build.VERSION.SDK_INT >=24) {
    fileURI = FileProvider.getUriForFile(context,
        "com.example.android.provider",
        localFile);
    intent.setDataAndType(fileURI, "image/jpeg");
} else {
    intent.setDataAndType(Uri.fromFile(localFile), "image/jpeg");
}
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);

Manifest file:

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

file_paths2.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
</paths>

I don't have any clue.

Log:

11-10 19:59:08.052 21800-21800/com.yannick.mychatapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.yannick.mychatapp, PID: 21800
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
    at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:605)
    at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:579)
    at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:417)
    at com.yannick.mychatapp.ChatRoom$17$1.onClick(ChatRoom.java:936)
    at android.support.design.widget.Snackbar$1.onClick(Snackbar.java:317)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
TheBC97
  • 31
  • 1
  • 7
  • 1
    Use LogCat to examine the Java stack trace associated with your crash: https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare Nov 10 '18 at 15:53
  • may be this question help you : https://stackoverflow.com/questions/41707436/opening-a-file-in-downloads-folder-android-napi-24 also see this :https://proandroiddev.com/sharing-files-though-intents-are-you-ready-for-nougat-70f7e9294a0b – Amir133 Nov 10 '18 at 16:03
  • @CommonsWare see below – TheBC97 Nov 10 '18 at 19:03
  • When you want to add material to a question, use the "edit" link below the question. Do not post an answer that is not an answer. – CommonsWare Nov 10 '18 at 19:07

1 Answers1

0
fileURI = FileProvider.getUriForFile(context,
    "com.example.android.provider",
    localFile);

Here, you use com.example.android.provider.

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

Here, you use com.example.android.fileprovider.

These are not the same. They need to be the same. And, they need to be something specific for your app. Use something based on your own app's package name, and use the same value in both places.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • ahhhh, thanks! Now I get a URI: content://com.example.android.fileprovider/Pictures/MyChatApp/MyChatApp_20181110_193713.jpeg but it still crashes :/ – TheBC97 Nov 10 '18 at 19:39
  • @TheBC97: Use LogCat, see what the stack trace is, and if it does not make sense, ask a separate Stack Overflow question. There, as part of a [mcve], you can provide your new code and the new stack trace. – CommonsWare Nov 10 '18 at 19:46