Opening File via Intent using Uri on API 24+









up vote
0
down vote

favorite












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)









share|improve this question



















  • 1




    Use LogCat to examine the Java stack trace associated with your crash: stackoverflow.com/questions/23353173/…
    – CommonsWare
    Nov 10 at 15:53










  • may be this question help you : stackoverflow.com/questions/41707436/… also see this :proandroiddev.com/…
    – amir133
    Nov 10 at 16:03











  • @CommonsWare see below
    – TheBC97
    Nov 10 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 at 19:07














up vote
0
down vote

favorite












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)









share|improve this question



















  • 1




    Use LogCat to examine the Java stack trace associated with your crash: stackoverflow.com/questions/23353173/…
    – CommonsWare
    Nov 10 at 15:53










  • may be this question help you : stackoverflow.com/questions/41707436/… also see this :proandroiddev.com/…
    – amir133
    Nov 10 at 16:03











  • @CommonsWare see below
    – TheBC97
    Nov 10 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 at 19:07












up vote
0
down vote

favorite









up vote
0
down vote

favorite











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)









share|improve this question















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)






android file android-intent uri






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 19:38

























asked Nov 10 at 15:42









TheBC97

63




63







  • 1




    Use LogCat to examine the Java stack trace associated with your crash: stackoverflow.com/questions/23353173/…
    – CommonsWare
    Nov 10 at 15:53










  • may be this question help you : stackoverflow.com/questions/41707436/… also see this :proandroiddev.com/…
    – amir133
    Nov 10 at 16:03











  • @CommonsWare see below
    – TheBC97
    Nov 10 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 at 19:07












  • 1




    Use LogCat to examine the Java stack trace associated with your crash: stackoverflow.com/questions/23353173/…
    – CommonsWare
    Nov 10 at 15:53










  • may be this question help you : stackoverflow.com/questions/41707436/… also see this :proandroiddev.com/…
    – amir133
    Nov 10 at 16:03











  • @CommonsWare see below
    – TheBC97
    Nov 10 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 at 19:07







1




1




Use LogCat to examine the Java stack trace associated with your crash: stackoverflow.com/questions/23353173/…
– CommonsWare
Nov 10 at 15:53




Use LogCat to examine the Java stack trace associated with your crash: stackoverflow.com/questions/23353173/…
– CommonsWare
Nov 10 at 15:53












may be this question help you : stackoverflow.com/questions/41707436/… also see this :proandroiddev.com/…
– amir133
Nov 10 at 16:03





may be this question help you : stackoverflow.com/questions/41707436/… also see this :proandroiddev.com/…
– amir133
Nov 10 at 16:03













@CommonsWare see below
– TheBC97
Nov 10 at 19:03




@CommonsWare see below
– TheBC97
Nov 10 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 at 19:07




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 at 19:07












1 Answer
1






active

oldest

votes

















up vote
0
down vote













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.






share|improve this answer




















  • 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 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 Minimal, Complete, and Verifiable example, you can provide your new code and the new stack trace.
    – CommonsWare
    Nov 10 at 19:46










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240556%2fopening-file-via-intent-using-uri-on-api-24%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
0
down vote













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.






share|improve this answer




















  • 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 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 Minimal, Complete, and Verifiable example, you can provide your new code and the new stack trace.
    – CommonsWare
    Nov 10 at 19:46














up vote
0
down vote













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.






share|improve this answer




















  • 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 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 Minimal, Complete, and Verifiable example, you can provide your new code and the new stack trace.
    – CommonsWare
    Nov 10 at 19:46












up vote
0
down vote










up vote
0
down vote









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.






share|improve this answer












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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 19:08









CommonsWare

756k13618441897




756k13618441897











  • 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 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 Minimal, Complete, and Verifiable example, you can provide your new code and the new stack trace.
    – CommonsWare
    Nov 10 at 19:46
















  • 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 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 Minimal, Complete, and Verifiable example, you can provide your new code and the new stack trace.
    – CommonsWare
    Nov 10 at 19:46















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 at 19:39





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 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 Minimal, Complete, and Verifiable example, you can provide your new code and the new stack trace.
– CommonsWare
Nov 10 at 19:46




@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 Minimal, Complete, and Verifiable example, you can provide your new code and the new stack trace.
– CommonsWare
Nov 10 at 19:46

















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53240556%2fopening-file-via-intent-using-uri-on-api-24%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

27

Top Tejano songwriter Luis Silva dead of heart attack at 64

Category:Rhetoric