java


FileUriExposed exception error


im getting the following error in my logcat.
: android.os.FileUriExposedException: file:///storage/emulated/0/download/AllCast.apk exposed beyond app through Intent.getData()
it only happening on some devices ie marsh mellow and nougat.
this is where error occurs
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" + newnamestring)), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
ive tried adding this line of code
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
also tried this in my app class
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
builder.detectFileUriExposure();
can anybody tell me what im doing wrong. thanks
EDIT****
Ok guys im trying to get #CommonsWare answer to work i can get the file to download fin to the provider folder. but then my app crashes when it trys to handle the intent. here is my new log cat
05-07 16:25:35.784 8715-8715/com.example.harrops.h20droidapp2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: mypackagename, PID: 8715
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.io.File.getName()' on a null object reference
at my package name.Casting_Apps$DownloadFileFromURL.onPostExecute(Casting_Apps.java:273)
which leads me to section of code
File newFile = null;
MimeTypeMap mime = MimeTypeMap.getSingleton();
String ext = newFile.getName().substring(newFile.getName().lastIndexOf(".") + 1);
String type = mime.getMimeTypeFromExtension(ext);
try {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Uri contentUri = FileProvider.getUriForFile(getBaseContext(), "my package name", newFile);
intent.setDataAndType(contentUri, type);
} else {
intent.setDataAndType(Uri.fromFile(newFile), type);
}
startActivityForResult(intent, ACTIVITY_VIEW_ATTACHMENT);
} catch (ActivityNotFoundException anfe) {
Toast.makeText(getBaseContext(), "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
}
This is the answer i went for.. thanks #CommonsWare for the help
File toInstall = new File(appDirectory, appName + ".apk");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(activity, BuildConfig.APPLICATION_ID + ".provider", toInstall);
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
activity.startActivity(intent)
} else {
Uri apkUri = Uri.fromFile(toInstall);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(apkUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
activity.startActivity(intent);
}

Related Links

Exporting a runnable jar from big eclipse Java project with JIBX too, works in Eclipse but cant figure out how to export it
JSP tags in a Freemarker template
Implementation of equals(): compare against implemented interface or implementing class?
Calculate business days difference between two days excluding holidays?
Alternative JSF implementation with JBoss 71
How to handle cyclic dependency in spring
checking if difference between 2 date is more than 20 minutes
Setting a JPanel contained in a JDesktopPane as always on top
Downloading on Android - FileNotFoundException but file exists
Hibernate hbm2ddl.auto update does not drop columns with mysql
Huge number of objects, sharding and java EE
java middleware webservices - exception handling
How to rename a file to another file system?
(How to manipulate)Poker Positioning using ENUM
question about interfaces and generics in Java: type mismatch error
Why can't I import static java.lang.System.out.println?

Categories

HOME
cakephp
netbeans
cookies
activiti
isabelle
office365api
relay
spagobi
youtube-dl
q
framework7
bookshelf.js
twitter-bootstrap-4
umd
acquia
elasticsearch-hadoop
collectd
task
decomposition
vaadin7
jsdoc
http-status-code-504
pythonanywhere
ab-testing
code-review
nhibernate-envers
reverse-proxy
sylius
tapestry
excel-2007
icloud-api
shopware
underflow
filezilla
librato
instant-messaging
directx-10
galsim
copying
azure-sql-database
bytecode-manipulation
android-ble
lto
xenforo
angularjs-factory
botbuilder
spring-security-kerberos
slick-3.0
businessworks
dotnetzip
veracode
jxcore
windows-mobile-6.5
pintos
angular-cache
file-writing
mikroc
trash
plottable.js
player
clang-static-analyzer
lttng
flash-cs5
iad
srand
reactive-banana
lemon
eclipse-clp
marching-cubes
content-length
computer-algebra-systems
map-projections
responsive-images
twirl
sortedlist
unity5.2.3
camanjs
gui-test-framework
cctv
uitouch
p4java
wp-query
mbr
floating-point-precision
.aspxauth
web2py-modules
ftps
buster.js
dataadapter
padarn
django-nonrel
random-seed
ticoredatasync
chrono
whoosh
locate
icanhaz.js
firefox4
backcolor
anti-piracy
caching-application-block

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App