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

Captcha validator; Client ip address in hibernate custom validator?
onActivityResult Failure delivering result ResultInfo
JAVA EE HIBERNATE CRITERIA API : add rows in database
Understanding ask, Whats the correct way using .save() and .commit() in Hibernate from the programming layers model Perspective
Get SearchHitField type using ElasticSearch Java API
netbeans 7.4 support for web application
EJB TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) doesn'
Java and Tomcat org.json java.lang.ClassNotFoundException
EntityManager.find not retunring values
IntelliJ IDE does not detect Scala templates properly
Struts 2 : Displays table of Songs - null gets printed
upload csv file containing million values to a particular column in mysql
MovableObject java syntax error
Deferred Result in Spring MVC returning incorrect response
Using google static maps - JFrame or Javascript
How to put a button texture on another texture?

Categories

HOME
testing
log4j
ngrx
appx
amortized-analysis
infragistics
malloc
sqlite3
disassembler
installshield
adfs
qt-creator
n-gram
dosgi
jsdoc
apache-metamodel
connection-string
jasonette
php-openssl
rundeck
smb
opennlp
centos6.5
windows-error-reporting
maxmind
r-raster
code-contracts
amazon-kinesis-kpl
bitbucket-pipelines
dynamics-crm-2013
streamsets
unspecified
hilbert-curve
angularjs-factory
avro4s
gulp-sourcemaps
heightmap
unixodbc
netcdf4
flashair
reactive-cocoa-5
logfiles
xml-documentation
rails-routing
ionicons
directory-structure
magma
np-complete
python-cryptography
epson
cudafy.net
nbconvert
or-tools
static-ip-address
android-fonts
nxt
heidisql
instant
transmitfile
plottable.js
wapiti
phpcas
srand
maven-tomcat-plugin
citrus-pay
jqgrid-formatter
tarjans-algorithm
angular-leaflet-directive
ionic
myo
processmodel
sortedlist
inmobi
bigbluebutton
block-device
modalpopup
qpainter
mesa
yorick
xceed-datagrid
srs
ccss
gridfs
tidy
gwt-rpc
qt-faststart
dotnethighcharts
jquery-ui-layout
pendrive
file-comparison
remember-me
createwindow
.nettiers
dentrix
asp.net-profiles
rescale
zend-tool
memory-size
kdbg
mtj

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