I am trying to add option to update app by downloading apk, its getting installed successfully. But the problem is, app got close after installation, without prompting Open/Done screen in Nougat and above devices.
I have tried using both ACTION_VIEW and ACTION_INSTALL_PACKAGE, but no luck. Also tried with startActivityForResult instead of startActivity, still no luck.
public void installAPK() {
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
File file = new File(path + "update.apk");
if (Build.VERSION.SDK_INT < 24) {
intent.setDataAndType(Uri.fromFile(new File(path + "update.apk")), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
} else {
Uri apkURI = FileProvider.getUriForFile(
activity,
activity.getApplicationContext()
.getPackageName() + ".provider", file);
intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
activity.startActivityForResult(intent, RC_INSTALL_APK);
}
Anything I missed to do?
Here's sample code of my application method to open new version
void OpenNewVersion(String location) {
Intent downloadIntent;
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
String PATH = Environment.getExternalStorageDirectory() + "/Download/";
File fileLocation = new File(PATH, "app-stock.apk");
Uri apkUri = FileProvider.getUriForFile(this, "Adapters.GenericFileProvider", fileLocation);
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
downloadIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(apkUri, "application/vnd.android.package-archive");
List<ResolveInfo> resInfoList = this.getPackageManager().queryIntentActivities(downloadIntent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
this.grantUriPermission(packageName, apkUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
} else {
File fileLocation = new File(this.getFilesDir(), "app-stock.apk");
downloadIntent = new Intent(Intent.ACTION_VIEW);
downloadIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
downloadIntent.setDataAndType(Uri.fromFile(fileLocation), "application/vnd.android.package-archive");
}
this.startActivity(downloadIntent);
}
Related
Currently, I need to update an installed application offline. To update the application, I have placed an apk file to the downloads folder of file manager and by pressing a button click event I want to update the apk but failed and facing an issue of "There was a problem while parsing the package error". I have rooted phone so is there any adb command or anything is available? I have also added exported="true" in manifest file and set allow to unknown permission for the app. Any help will be appreciable.
private void installApk() {
try {
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/Demo.apk";
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
if (Build.VERSION.SDK_INT >= 24) {
Uri downloaded_apk = FileProvider.getUriForFile(MainActivity.this, getApplicationContext().getPackageName() + ".provider", file);
intent.setDataAndType(downloaded_apk, "application/vnd.android.package-archive");
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
grantUriPermission(getApplicationContext().getPackageName() + ".provider", downloaded_apk, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
// startActivity(intent);
} else {
intent.setAction(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
startActivity(intent);
} catch (Exception e) {
e.printStackTrace();
}
}
minsdk is 19 and maxsdk is 33
I appreciate your help so that my code can open a PDF file with an Intent, this is the code i have been using:
String pathFile =
Environment.getExternalStorageDirectory().toString() +
FileUtils.PATH_SEPARATOR + "myFolder" +
FileUtils.PATH_SEPARATOR + "myFile.pdf";
File pdfFile = new File( pathFile );
if ( pdfFile.exists() ) {
Uri uri = Uri.fromFile( new File( pathFile ) );
Intent intent = new Intent( Intent.ACTION_VIEW );
intent.setDataAndType(uri, "application/pdf");
intent.addFlags( Intent.FLAG_ACTIVITY_NEW_TASK );
startActivity( intent );
}
But I understand that now (from the Android-N version) the privileges are handled differently and so i hope you can help me by indicating what changes i have to make to get a PDF file opened using an Intent.
PS: In my AndroidManifest.xml I am looking for all permissions in the STORAGE:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The answer was found at: http://android.2317887.n4.nabble.com/FileProvider-td394588.html
Terrific!.
Try this code.
Intent intent = new Intent(Intent.ACTION_VIEW);
try {
String newFilePath = filePath.replaceAll("%20", " ");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
intent.setDataAndType(Uri.parse(newFilePath), "application/pdf");
} else {
Uri uri = Uri.parse(newFilePath);
File file = new File(uri.getPath());
if (file.exists()){
uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
intent.setDataAndType(uri, "application/pdf");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
I want to open specific path folder using Intent, I used code for File explorer and It working perfectly but in some device (samsung devices) if file explorer app are not available then It not opening folder of specific path.
I tried many solutions but It won't work for me.
Uri uri = Uri.fromFile(new File(new File(filePath).getParent()));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, "resource/folder");
if (intent.resolveActivityInfo(getPackageManager(), 0) != null)
{
startActivity(intent);
}
else {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setDataAndType(uri, "file/*");
startActivity(Intent.createChooser(intent, "Open folder"));
}
Here is my solution for accessing "root/Downloads/Sam" in Kotlin
val rootPath = "content://com.android.externalstorage.documents/document/primary:"
val samPath = Uri.parse("$rootPath${Environment.DIRECTORY_DOWNLOADS}%2fSam") //"%2f" represents "/"
val REQUEST_CODE_PICK_FILE = 1
main_sam_button.setOnClickListener {
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = "text/plain" //specify file type
intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, samPath) //set the default folder
startActivityForResult(intent, REQUEST_CODE_PICK_FILE)
}
Demo:
https://youtu.be/lUIPyC_8q_M
Helpful reading:
"content://com.android.externalstorage.documents/document/primary:":
What is com.android.externalstorage?
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.setDataAndType(
Uri.parse(
(Environment.getExternalStorageDirectory().path
+ java.io.File.separator) + "myFile" + java.io.File.separator
), "*/*"
)
intent.addFlags(
Intent.FLAG_GRANT_READ_URI_PERMISSION
and Intent.FLAG_GRANT_WRITE_URI_PERMISSION
and Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION
and Intent.FLAG_GRANT_PREFIX_URI_PERMISSION
)
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(intent, WRITE_ACCESS_CODE)
I'm creating an app, and in this app have a button to open an PDF with a calendar with activities of user...
The PDF is in assets folder.
And the actualy code is:
private void abrirCalendario() {
AssetManager assetManager = getAssets();
File fileDir = new File(Environment.getExternalStorageDirectory().getPath() + "/");
fileDir.mkdirs();
File file = new File(fileDir, "Calendario DAERG.pdf");
String path = Environment.getExternalStorageDirectory().getPath() + "/Calendario DAERG.pdf";
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = FileProvider.getUriForFile(this, getPackageName() + ".provider", new File(path));
List<ResolveInfo> resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = "tk.davidev.android.ews";
grantUriPermission(packageName, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}else {
uri = Uri.fromFile(new File(path));
}
if(!file.exists()) {
try {
InputStream in = assetManager.open("calendario_2017.pdf");
OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
copyFile(in, out);
in.close();
out.flush();
out.close();
} catch (Exception e) {
}
}
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
And the error i'm getting is:
file:///storage/emulated/0/Calendario DAERG.pdf exposed beyond app through Intent.getData()
Hi because Android from Mashmellow , I think, changes its segurity behavior such as request for permission on runtime or using FileProvider to manage URI. Now me have to give permission in some code in order to work.
Here's an example of open Intent with ACTION_VIEW in Nougat
File archivo= new File(getExternalFilesDir(Environment.DIRECTORY_PICTURES),"excerpt_DEC.pdf");
Uri uri = FileProvider.getUriForFile(this,
"com.example.android.fileprovider",
archivo);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri,"application/pdf");
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivity(intent);
My example is the same as yours. I read a file from assets then, I find that file created and save it in archivo object. Later on, get an uri with FileProvider method and finally, I give permissions to intent with this line
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
I leave you this link for background knowledge about this new implementation.
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
Also, I followed this anwser to read my file.pdf from asset
https://stackoverflow.com/a/7495078/8650422
Hope to help, greetings !
I am programatically installing APK and using following code for that :
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
Uri apkUri = FileProvider.getUriForFile(getApplicationContext(),
BuildConfig.APPLICATION_ID + ".fileprovider", new File(Environment
.getExternalStorageDirectory() + "/.QCApp/" +
Constants.APP_TYPE + versionName + ".apk"));
Intent intent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
intent.setData(apkUri);
intent.putExtra(Intent.EXTRA_NOT_UNKNOWN_SOURCE, true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory() +
"/.QCApp/" +
Constants.APP_TYPE + versionName + ".apk")),
"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
But in Android Nougat APK getting successfully installed but "Done" and "Open" screen is not appearing.
What I'm missing ?