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

DataAccessException couldn't catch
Overriding toString() in Exception Handling
How to center align everything in JPanels having GridLayout
Showing Morning, afternoon, evening, night message based on Time in java
instance method reference. no suitable method found for
Tomcat 7.0 HTTP post request utf-8 issues with windows server 2008 R2
Is there a BiMap implementation with predictable iteration ordering (insertion order)?
Eclipse says “Constructor is undefined”
AlarmManager can't be cancelled in the Activity
Login to PHP based website via Java Application
JButton does not return the right text value
Regex to extract numbers and group them with thousands separator
Properly use the RelationshipEntity
Setting Cursor for App Engine Datastore using Java
Intermittent error org.hibernate.PersistentObjectException: detached entity passed to persist
Localizing a legacy product?

Categories

HOME
hp-exstream
session
relayjs
yahoo-oauth
infragistics
ezpublish
webrequest
iggrid
google-cloud-spanner
usergrid
msp430
beyondcompare
shared-hosting
firefox-webextensions
dbext
autocad-plugin
abi
intentfilter
sylius
kvc
format-specifiers
h2db
fifo
newline
bpel
preg-match
google-rich-snippets
oracle-fusion-middleware
copying
typed.js
vsts-build-task
mapbox-gl
mpmediaquery
nssegmentedcontrol
dynamics-crm-2013
android-browser
service-discovery
unoconv
grid.mvc
dynamic-reports
http-redirect
gpx
segment
angular-resource
komodoedit
prolog-setof
multipeer-connectivity
paxos
winscp-net
dropbox-php
mu
forever
kbuild
crypt
font-size
qtwebview
cudafy.net
dwscript
xcb
firmata
bonobo
trash
storekit
cloudbees
ora-00900
tmuxinator
citrus-pay
ideamart
key-management
bridge.net
nsmutabledictionary
xc16
nstableviewcell
qpainter
html-helper
zend-route
openexr
sgen
elliptic-curve
mechanize-ruby
dealloc
hyprlinkr
balanced-payments
ember-charts
intentservice
quantlib-swig
ms-project-server-2010
gwt-rpc
gridcontrol
sabredav
factory-method
appconkit
qtkit
reddot
subscript
tomcat-valve
netbeans-7.1
disclosure
blitz++
premature-optimization
sector
ubuntu-9.04

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