I want to show a notification and when user clicked install an APK file.
I know how to use the fileProvider itself but I have problem with service context.
Here is my code
manifest:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.company.app"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/filepaths" />
</provider>
filePaths:
<paths>
<external-path
name="externalFiles"
path="." />
</paths>
service Code:
resultIntent = new Intent(Intent.ACTION_VIEW);
Uri uri;
File file = FileHelper.getApkFile(item.getLink());
if (Build.VERSION.SDK_INT >= 24) {
uri = FileProvider.getUriForFile(
NotificationService.this,
NotificationService.this.getApplicationContext().getPackageName(),
file);
NotificationService.this.setResult(Activity.RESULT_OK,
resultIntent);
} else {
uri = Uri.fromFile(file);
}
resultIntent.setDataAndType(uri, "application/vnd.android.package-archive");
resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
as you know setResult() is not available in service.
How can I grant permission from service context?
thank you every one.
I found the problem
I had to add this permission to my manifest to install apk file.
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
Related
I have created a html page with tables that lists android apks.
I also have an APK which displays the html page, when clicking the download button it downloads the chosen APK... Perfect!
What I want to do is once the download is complete it requests to install the application.
I've tried a few things but it's too much for me to do, would anyone mind helping for some beer tokens,
Thanks
You can add a BroadcastReceiver as a callback when your download completes , and then call the following method inside that callback-
public static void installapk(File apkFile, Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (apkFile.exists()) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
install.setDataAndType(Uri.fromFile(apkFile),
"application/vnd.android.package-archive");
Uri apkUri =
FileProvider.getUriForFile(context, "com.yourcompany.yourapp.fileprovider", apkFile);
install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
install.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
install.setDataAndType(apkUri, "application/vnd.android.package-archive");
context.startActivity(install);
} else {
install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
install.setDataAndType(Uri.fromFile(apkFile),
"application/vnd.android.package-archive");
context.startActivity(install);
}
} else {
System.out.println("apk doesnot exist.");
}
}
}
Don't forget to add this permission in your AndroidManifest.xml file -
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
You also need to create a fileprovider authority in your AndroidManifest.xml -
<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"/>
</provider>
Then Create a new xml File inside res/xml folder named file_paths.xml and paste this code -
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<files-path
name="files"
path="." />
</paths>
Happy Coding!
I am trying to install apk after it has been downloaded by the download manager. I am using a broadcast to get the message after download completes. This is my code below:-
FileProvider: Installing APK. There was an error parsing the package.
val file = File(getExternalFilesDir("Download"), "/playlist/update.apk")
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val apkUri = FileProvider.getUriForFile(this#MainActivity, BuildConfig.APPLICATION_ID + ".provider", file)
val intent = Intent(Intent.ACTION_INSTALL_PACKAGE)
intent.data = apkUri
intent.flags= Intent.FLAG_GRANT_READ_URI_PERMISSION
this#MainActivity.startActivity(intent)
} else {
val apkUri = Uri.fromFile(file)
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(apkUri, "application/vnd.android.package-archive")
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
this#MainActivity.startActivity(intent)
}
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/filepaths" />
</provider>
filespath.xml :-
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-files-path name="Download" path="Download/" />
</paths>
I am downloading the apk in "Downloads/playlist/" dir. I saw this same problem - here. However I have can't seem to get it working from that solution. I also checked the apk by installing it myself and it gets installed without any errors. Please help.
My app crashed when open camera in Android 7.0
File captureFilePath = new File(cachePath, CommonUtil.getDateTimeForFileName(System.currentTimeMillis())+".jpg");
Uri uri = Uri.fromFile(captureFilePath);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(intent, REQUEST_CAPTURE_IMAGE);
CAPTURE_IMAGE_PATH = captureFilePath.getAbsolutePath();
If your targetSdkVersion >= 24, then we have to use FileProvider class to give access to the particular file or folder to make them accessible for other apps.
1) First Add a FileProvider tag in AndroidManifest.xml under tag as below:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
...
<application
...
<provider
android:name=".GenericFileProvider"
android:authorities="${applicationId}.my.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
</application>
</manifest>
2) Then create a provider_paths.xml file in res/xml folder. Folder may be needed to created if it doesn't exist.
<paths>
<external-path name="external_files" path="."/>
</paths>
3) Now edit your activity class file as below:
Uri uri = Uri.fromFile(captureFilePath);
to
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, captureFilePath);
I am trying to install an .apk I have downloaded to the downloads folder in Android 7.
I have tried the way recommended in a number of StackOverflow posts and here https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en by using a FileProvider:
File file = new File(fileUri);
//using Android.Support.V4.Content;
var downloadUri = FileProvider.GetUriForFile(context,context.ApplicationContext.PackageName + ".com.package.name.provider", file);
Intent install = new Intent(Intent.ActionInstallPackage);
install.AddFlags(ActivityFlags.GrantReadUriPermission);
install.AddFlags(ActivityFlags.GrantWriteUriPermission);
install.AddFlags(ActivityFlags.GrantPersistableUriPermission);
install.SetDataAndType(downloadUri, "application/vnd.android.package-archive");
context.StartActivity(install);
AndroidManifest.xml
<uses-permission android:name="android.permission.INSTALL_PACKAGES" />
<application android:label="Settings" android:icon="#drawable/Icon" android:theme="#style/myTheme">
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.com.package.name.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/provider_paths"/>
</provider>
</application>
provider_paths.xml
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="." />
</paths>
The "downloadUri" looks like: "content://com.package.name.com.package.name.provider/external_files/Download/Sensors%2520Multitool_1.3.0_apk-dl.com.apk"
The error when the installation window pops up is: "There was a problem parsing the package".
I have installed this package by clicking on it in the downloads folder and it installs fine, I have also tried other .apk's with the same issue.
File file = new File(fileUri);
if(Build.VERSION.SdkInt >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.GetUriForFile(context, context.ApplicationContext.PackageName + ".provider", toInstall);
Intent intentS = new Intent(Intent.ActionInstallPackage);
intentS.SetData(apkUri);
intentS.SetFlags(ActivityFlags.GrantReadUriPermission);
context.StartActivity(intentS);
} else {
Uri apkUri = Uri.FromFile(toInstall);
Intent intentS = new Intent(Intent.ActionView);
intentS.SetDataAndType(apkUri, "application/vnd.android.package-archive");
intentS.SetFlags(ActivityFlags.NewTask);
context.StartActivity(intentS);
}
It appears that the issue in parsing the package was due to the space in the package name "Sensors%2520Multitool_1.3.0_apk-dl.com.apk".
As soon as the space was removed the package installed correctly.
I'm taking a picture on Android Nougat with FileProvider, that's my code
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.mypackage.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="#xml/file_paths" />
</provider>
file_paths.xml:
<paths>
<files-path name="img" path="images/" />
</paths>
Java:
String fileName = "cameraOutput" + System.currentTimeMillis() + ".jpg";
File imagePath = new File(getContext().getFilesDir(), "images");
File file = new File(imagePath, fileName);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
final Uri outputUri = FileProvider.getUriForFile(activity, "com.mypackage.fileprovider", file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
getContext().grantUriPermission(
"com.google.android.GoogleCamera",
outputUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION
);
cameraIntent.setFlags(FLAG_GRANT_WRITE_URI_PERMISSION);
activity.startActivityForResult(cameraIntent, ACTIVITY_REQUEST_CODE);
Camera activity starts, take picture successfully, but the Activity Result is not OK and the only thing I see in error logs is
CAM_StateSavePic: exception while saving result to URI: Optional.of(content://com.mypackage.fileprovider/img/cameraOutput1469383289530.jpg)
FileNotFoundException: Is a directory
EDIT
missed File#createNewFile();
The main thing to get camera and gallery URI working is provider paths.
you need to create a provider_paths.xml to /res/xml/ dir
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="external_files" path="."/>
</paths>
In your manifest file add this lines between
<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>
One more thing, I've found that we need to set vmPolicy in Application class like this
#Override
public void onCreate() {
super.onCreate();
//Allowing Strict mode policy for Nougat support
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
}
Double check your camera and external storage permission in manifest file and there you go with nougat URI.
For more details check this link : Android N FileUriExposedException
if androidx was used in your project, you need replace dot(.) to slash(/).
<!--别用. 不然在androidx上面有问题-->
<external-path name="sdcard" path="/" />
works for me great!