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)
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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