My current manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cecop.cecop" android:installLocation="auto" android:versionCode="132" android:versionName="14.00">
<uses-sdk android:minSdkVersion="23" android:targetSdkVersion="31" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<application android:label="cecop" android:icon="#drawable/cecop">
<!--Especifique el número de versión de Google Play Services en el manifiesto y la clave de Google Maps V2 API. -->
<meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyBLH0HotP0r5yfuX8xFESZhM5TS0PcluKs" />
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
<uses-library android:name="org.apache.http.legacy" android:required="false" />
<!-- Needed for Android >= Nougat for file access -->
<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.cecop.cecop.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/file_paths"></meta-data>
</provider>
<service android:name="com.cecop.cecop.ServicioSegundoPlano" android:exported="true"></service>
</application>
</manifest>
In the service and main activity I have this attributes
[Service(Exported = true, Name = "com.cecop.cecop.ServicioSegundoPlano")]
public class ServicioStartForeground : Service
{
...
}
[Activity(Label = "Cecop", Icon = "#drawable/icon", Theme = "#style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{...
}
As I write in the manifest so you don't get the error in the publication on Google Play?
You have uploaded an APK or Android App Bundle that has an activity, an alias of activity, a service or an emission receiver with atnts filter, but without establishing the Android property: exported. This file cannot be installed on Android 12 or later versions.
You don't have to define anything in AndroidManifest.xml if you are using the [Service] attribute. Xamarin generates this for you.
The only change you need to make on Android 12, is that you need to add whether a Service is exported or not. Default should be false unless you need to access it from another App than yours.
If you are using something like Xamarin.Essentials or other libraries that also contain services, make sure they are up to date and also follow these rules about adding Exported property on their services.
I have a PDF file I download from an API and save to internal storage using Environment.SpecialFolder.MyDocuments. When I attempt to open the file I get the error in the title, Failed to find configured root that contains /data/data/com.app.myapp/files/myfile.pdf.
Code that saves files:
var filePath = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
System.IO.File.WriteAllBytes(filePath, fileBytes);
LaunchIntent(filePath);
Code where error occurs:
public static void LaunchIntent(string fileLocation)
{
// now create an activity which points to the file
var localPath = global::Android.Net.Uri.Parse(fileLocation).Path;
File javaFile = new File(localPath);
//error occurs at below code
var filePath = global::Android.Support.V4.Content.FileProvider.GetUriForFile(
CrossCurrentActivity.Current.Activity,
CrossCurrentActivity.Current.Activity.PackageName + ".fileprovider",
javaFile);
var actionView = new Intent(Intent.ActionView);
actionView.AddFlags(ActivityFlags.GrantReadUriPermission);
var extension = localPath.Substring(localPath.LastIndexOf('.') + 1);
var mimeType = MimeTypeMap.Singleton.GetMimeTypeFromExtension(extension);
actionView.SetDataAndType(filePath, mimeType);
CrossCurrentActivity.Current.Activity.StartActivity(actionView);
}
My file paths file:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external" path="." />
<external-files-path name="external_files" path="." />
<cache-path name="cache" path="." />
<external-cache-path name="external_cache" path="."/>
</paths>
According to your description, you pdf is saved in internal storage, Provider path for a specific path as followings:
<files-path/> --> Context.getFilesDir()
<cache-path/> --> Context.getCacheDir()
<external-path/> --> Environment.getExternalStorageDirectory()
<external-files-path/> --> Context.getExternalFilesDir(String)
<external-cache-path/> --> Context.getExternalCacheDir()
<external-media-path/> --> Context.getExternalMediaDirs()
You can refer to: https://developer.android.com/reference/androidx/core/content/FileProvider
So change your resources/xml/file_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
<files-path name="internal_files" path="." />
</paths>
And adding a provider in the AndroidManifest.xml file Application tag.
<provider android:name="androidx.core.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true">
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="#xml/file_paths"></meta-data>
</provider>
</application>
I'm creating Xamarin.Forms App and it has a service that runs in the background monitoring if any messages arrive. If a message arrives, the service should open the App.
I tried to open the App using Intent (Dependency Service) and I also tried using Xamarin.Essentials. Neither way worked.
My last attempt was with Xamarin.Essentials and gave the following error:
[Error][1]
[1]: https://i.stack.imgur.com/5jPGf.png
This is my code using Intent (Dependency Service):
//Intent (Dependency Service)
string teste = "com.saferit.saferproject";
openAppManager = DependencyService.Get<IAppHandler>();
openAppManager.Initialize(teste);
openAppManager.LaunchApp();
[enter image description here][2]
[2]: https://i.stack.imgur.com/lLx3g.png
This is my native code for Android
public void LaunchApp()
{
Intent intent = new Intent();
intent.SetClassName(Package, "activity");
intent.AddFlags(ActivityFlags.NewTask);
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
[Complete Native Code][3]
[3]: https://i.stack.imgur.com/G4zOV.png
This is my AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.saferit.saferproject" android:installLocation="preferExternal">
<uses-sdk android:minSdkVersion="27" android:targetSdkVersion="29" />
<!--<application android:label="SaferProject.Android"></application>-->
<application android:label="com.saferit.saferproject" android:icon="#drawable/logo">
<activity android:icon="#drawable/logo" android:label="com.saferit.saferproject" android:name="com.saferit.saferproject">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="com.saferit.saferproject" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
This is my attempt using Xamarin.Essential:
//Xamarin.Essentials
var appname = "com.saferit.saferproject://";
var supportsUri = await Launcher.CanOpenAsync(appname);
if (supportsUri)
await Launcher.OpenAsync(appname);
Looking for here at stackoverflow I found this question:
Open Notification list page when user clicks on navigation
I tried to implement it on my App but I also couldn't get it to work.
In all three cases the errors are very similar.
I think the problem is in the configuration of the URI of my App.
Can someone help me?
I am trying to get my app to automatically install an apk. This works fine for api<24. But for 24, it is failing. Android has implemented extra security:
For apps targeting Android 7.0, the Android framework enforces the StrictMode API policy that prohibits exposing file:// URIs outside your app. If an intent containing a file URI leaves your app, the app fails with a FileUriExposedException exception.
So I tried this:
Uri myuri;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N){
myuri = Uri.parse("file://"+outapk);
} else {
File o = new File(outapk);
myuri = FileProvider.getUriForFile(con, con.getApplicationContext().getPackageName() + ".provider", o);
}
Intent promptInstall = new Intent(Intent.ACTION_VIEW).setDataAndType(myuri,"application/vnd.android.package-archive");
con.startActivity(promptInstall);
but get a fatal exception:
com.android.packageinstaller "Caused by: java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{b42ee8a 6826:com.android.packageinstaller/u0a15} (pid=6826, uid=10015) that is not exported from uid 10066".
I have export=true in my manifest.
The problem seems to be that packageinstaller cannot use a content:// uri.
Are there any ways to allow an app to progammatically install an apk with api24?
Are there any ways to allow an app to progammatically install an apk with api24?
Add addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION) to your promptInstall setup, to grant read access to the content.
I have export=true in my manifest.
Not on your FileProvider, as that would cause your app to crash.
The problem seems to be that packageinstaller cannot use a content:// uri.
No, the problem is that you did not grant permission to the package installer to read from that Uri. Had the package installer been unable to use a content scheme, you would have gotten an ActivityNotFoundException.
Note, though, that it is only with Android 7.0 that the package installer starts supporting content. Earlier versions of Android have to use file.
For Oreo, Add permission in AndroidManifast (Otherwise it just silently fails)
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
now add to you'r Manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
in xml directory add...
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." /></paths>
then use these codes where you want.
File directory = Environment.getExternalStoragePublicDirectory("myapp_folder");
File file = new File(directory, "myapp.apk"); // assume refers to "sdcard/myapp_folder/myapp.apk"
Uri fileUri = Uri.fromFile(file); //for Build.VERSION.SDK_INT <= 24
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", file);
}
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); //dont forget add this line
context.startActivity(intent);
}
For Oreo,
Add permission in AndroidManifast
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
Here is the solution I have found
val newFile = File(dirPath, "$fileNameWithoutExtn.apk")
var fileUri = Uri.fromFile(newFile)
//use the fileProvider to get the downloaded from sdcard
if (Build.VERSION.SDK_INT >= 24) {
fileUri = FileProvider.getUriForFile(this#SettingAcitivity, applicationContext.packageName + ".provider", newFile)
val intent=Intent(Intent.ACTION_VIEW)
intent.setDataAndType(fileUri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_GRANT_READ_URI_PERMISSION
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent)
}else{
newFile.setReadable(true, false)
val intent = Intent(Intent.ACTION_VIEW)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
intent.setDataAndType(fileUri, "application/vnd.android.package-archive")
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
startActivity(intent)
}
and write in manifest
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/paths"/>
and also set the permission
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
and in xml folder paths will be
<paths>
<external-path
name="external_files"
path="." />
</paths>
Add file in res/xml -> provider_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
Add this code in AndroidManifest.xml
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.provider" <-- change this with your package name
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
run this code for install your app or open
public void installApk(String file) {
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider",new File(file));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
context.startActivity(intent);
}
simply do the following steps:
1- add the following permission to manifest:
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES"/>
2- Add provider to manifest (as child as application tag):
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="tatcomputer.ir.libraryapp.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/paths"/>
</provider>
3- Add paths.xml to xml directory:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="files" path="." />
<external-files-path name="external_files" path="." />
<external-path name="external_files" path="." />
<cache-path name="cached_files" path="." />
<external-cache-path name="cached_files" path="." />
</paths>
4- show install apk page using following code (note that in my case apk is in root of my phone named tmp.apk:
String root = Environment.getExternalStorageDirectory().toString();
Uri fileUri24 = FileProvider.getUriForFile(App.applicationContext, BuildConfig.APPLICATION_ID + ".provider", new File(root + "/tmp.apk"));
Uri fileUri = Uri.fromFile(new File(root + "/tmp.apk"));
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24)
intent.setDataAndType(fileUri24, "application/vnd.android.package-archive");
else intent.setDataAndType(fileUri, "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
App.applicationContext.startActivity(intent);
Intall Apk automatically
Intent intent1 = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent1.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent1.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/download/" +filename)), "application/vnd.android.package-archive");
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
In my case on android 8.0 problem was in
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
More detailed how to get this permission Exception 'open failed: EACCES (Permission denied)' on Android
Update in 2022
You can't use internal storage to install an APK. You have to use External Storage instead.
And this is for people looking at how to install step-by-step APK on a remote Android device. Also, the tutorial has a NodeJS Server implementation.
Remote install for Android app using APK
I am trying to generate PDF report with Jasper Reports using Spring. But report is always empty. I searched a lot and could not find problem. I tried to write byte[] pdfReport to OutputStream but result is same, report is always empty.
I have 2 parameters one o them is testName and one of them is chart. Both can not display at pdf report.
Thanks a lot for your help,
Here is the code that generates report.
//My class extends MultiActionController
DefaultPieDataset dataset = new DefaultPieDataset();
dataset.setValue(String.format("%s, %s", "pie1", "pie1"),20);
dataset.setValue(String.format("%s, %s", "pie2", "pie2"),80);
JFreeChart chart = ChartFactory.createPieChart("testPie", dataset, true, true, false);
Map model = new HashMap();
model.put("chart", chart.createBufferedImage(200, 200));
model.put("testName", "test report");
model.put("format", "pdf");
AbstractJasperReportsView view = new JasperReportsMultiFormatView();
view.setUrl("/WEB-INF/classes/reports/"+"test1"+".jasper");
view.setApplicationContext(getApplicationContext());
view.setContentType("application/pdf");
Properties header = new Properties();
view.setHeaders(header);
ModelAndView mv = new ModelAndView(view, model);
JasperReport report = (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));
JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);
return mv;
And here is jrxml file
<?xml version="1.0" encoding="UTF-8"?>
<jasperReport xmlns="http://jasperreports.sourceforge.net/jasperreports" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jasperreports.sourceforge.net/jasperreports http://jasperreports.sourceforge.net/xsd/jasperreport.xsd" name="test1" language="groovy" pageWidth="700" pageHeight="842" columnWidth="660" leftMargin="20" rightMargin="20" topMargin="20" bottomMargin="20">
<property name="ireport.zoom" value="1.0"/>
<property name="ireport.x" value="0"/>
<property name="ireport.y" value="2"/>
<parameter name="testName" class="java.lang.String"/>
<parameter name="chart" class="java.awt.Image" isForPrompting="false"/>
<background>
<band splitType="Stretch"/>
</background>
<title>
<band height="48" splitType="Stretch"/>
</title>
<pageHeader>
<band height="35" splitType="Stretch"/>
</pageHeader>
<columnHeader>
<band height="14" splitType="Stretch"/>
</columnHeader>
<detail>
<band height="229" splitType="Stretch">
<image>
<reportElement x="162" y="13" width="200" height="200"/>
<imageExpression class="java.awt.Image"><![CDATA[$P{chart}]]></imageExpression>
</image>
<textField>
<reportElement x="37" y="61" width="100" height="20"/>
<textElement/>
<textFieldExpression class="java.lang.String"><![CDATA[$P{testName}]]></textFieldExpression>
</textField>
</band>
</detail>
<columnFooter>
<band height="112" splitType="Stretch"/>
</columnFooter>
<pageFooter>
<band height="76" splitType="Stretch"/>
</pageFooter>
<summary>
<band height="42" splitType="Stretch"/>
</summary>
</jasperReport>
Why are you doing the following?
ModelAndView mv = new ModelAndView(view, model);
JasperReport report = (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));
JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);
return mv;
Using JasperReportsMultiFormatView() already combines the filling and exporting of the report. No need for the following:
JasperReport report = (JasperReport) JRLoader.loadObject(getServletContext().getResourceAsStream("/WEB-INF/classes/reports/"+"test1"+".jasper"));
JasperPrint prt = JasperFillManager.fillReport(report, model);
byte[] pdfReport = JasperExportManager.exportReportToPdf(prt);
You must declare the JRXML file and report data key in a resource bundle.
I suggest you check this guide I wrote for Spring 3 MVC - Jasper Integration Tutorial:
http://krams915.blogspot.com/2010/12/spring-3-mvc-jasper-integration.html
Just modify the views to use the JasperReportsMultiFormatView() instead.
Check the Tutorials section for more. If I have time later, I'm gonna make a tutorial using JasperReportsMultiFormatView(). If not maybe tomorrow. Let me know if this helps you.
There is a property in jasper report called 'whenNoDataType'. Did you tried setting that to
whenNoDataType="AllSectionsNoDetail"
Also the report language is given as 'groovy'. Why?