I am trying to install an apk file programmatically. I referred this site for doing it.
Now the apk file is downloaded from an url and placed in SDCard. When I try to install it on the device using Intent method,
It is popping up following error
Emulator:
"Parse Error - There is a problem parsing the package",
Logcat(W) : "Unable to read AndroidManifest.xml of /mnt/sdcard/download/myapp.apk",
Logcat(W) : "java.ioException: AndroidManifest.xml",
Logcat(W) : "Parse error when parsing manifest. Discontinuing installation".
Logcat(E) : "java.lang.SecurityException",
Logcat(E) : "java.lang.NullPointerException".
Note: 1) I was able to install the same .apk file using adb.
2) I changed this .apk file to .pdf format before sending it to Server Team and then they changed it back to .apk. I am adding this coz there might be chances for error due to format conversion.
Installation can give the specified error at least in following cases:
Name of the package is changed after signing: Use the exact name as
the signed package is (instead, adjust the name in Manifest)
Package is compiled against on higher API level: Correct the API
level in Manifest file
Package is executed from SD-card: Run (install) the apk -file
from phones memory OR use adb command to install it
Please check this code
private void installApk(){
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File("/sdcard/path"));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);}
Related
I've been trying to find out how to install stored APK from internal memory, but I continually get errors.
I've tried getFilesDir() but it returns
08-14 03:18:09.127 10089-10089/? W/zipro: Error opening archive /data/user/0/package_name/files/app-release.apk: I/O Error
08-14 03:18:09.127 10089-10089/? D/asset: failed to open Zip archive '/data/user/0/package_name/files/app-release.apk'
08-14 03:18:09.127 10089-10089/? W/PackageInstaller: Parse error when parsing manifest. Discontinuing installation
I've also tried Environment.getDownloadCacheDirectory() but I get a similar result.
I am attempting to install on a non-rooted device, but I have signature level app permission.
How do I install app on a non-rooted device?
Even
public void installAPKManual(String filename) {
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(filename));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
}
doesn't work. Parse error.
Update
It works after I use
file.setReadable(true, false)
to get world readable permission, but I wonder if there is a better way?
If you are trying to retrieve the file from Environment then you need storage permission.
or if you are trying to get the file from your private storage getCacheDir() or getFileDir() you don't need permission but you must use File Provider.
if these are not the case try to install the APK manually first and see that everything is ok with the APK or not!!!
I have some lines of code and apk file in internal cache dir:
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(newFile(context.getCacheDir() + "/update.apk")), "application/vnd.android.package-archive");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
But I had a parsing error: "There was a problem parsing the package"
Thank You for helping!
Third-party apps, including the package installer, have no access rights to that file.
On Android N, they have finally fixed the problem with installing apps via a ContentProvider, like FileProvider. For versions prior to that, you can install from external storage, or maybe use MODE_WORLD_READABLE (deprecated, and banned on Android N) to allow that file to be read from internal storage.
recently i am working on a android apps build in phonegap, i am quite new with mobile development and this i smy first appsbut, and i am stuck when try to download some documents (pdf, doc, jpg) to local storage and open it, after search through google and i tried to follow this solution.
I followed the exact code in the example and below is how i call the plugins:
window.plugins.downloader.downloadFile(url,'/sdcard/download/', 'test.pdf', true, downloadOkCallbak, downloadErCallbak);
window.plugins.pdfViewer.showPdf('/sdcard/download/test.pdf');
The url is my remote file, when i execute it, i got error: "TypeError: window.plugins is undefined". Anyone help is appreciated.
[update] My code of showPdf function:
public String showPdf(String fileName) {
File file = new File(fileName);
if (file.exists()) {
try {
Uri path = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//intent.setData(Uri.parse(fileName));
this.ctx.startActivity(intent);
return "";
} catch (android.content.ActivityNotFoundException e) {
System.out.println("PdfViewer: Error loading url "+fileName+":"+ e.toString());
return e.toString();
}
}else{
return "file not found";
}
}
[Update 2] I got below error in web console: Uncaught ReferrenceError: LocalFileSystem is not defined at ...
What could be the issue?
I had the same problem as the OP and many unsuccessful approaches later I came to the following solution (tested with Cordova 3.1.0-3.3.0 & Samsung Galaxy S3 & iOS 7):
Solution
First, get the file by requesting the file system and then download it from your server with fileTransfer.download(). After completing the download call the FileOpener (which will open a pop up where to choose from apps like Adobe Reader). Note: Make sure to include the FileTransfer feature via CLI/Terminal: cordova plugin add org.apache.cordova.file
Navigate to your project folder
Open CLI/Terminal and type in: cordova plugin add https://github.com/don/FileOpener.git
After successful download and storage of your file on the device (see above) you open it with: window.plugins.fileOpener.open("file:///sdcard/Android/data/com.example.application/document.doc") or the appropriate path on iOS.
That's it! Good luck!
Make sure you've had these lines in your manifest.xml :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
You can find help here How to download a pdf file in Android?
Make sure
You should install git and install file plugins(cordova 3.0+)
File and file transfer plugin
$ cordova plugin add org.apache.cordova.file
$ cordova plugin add org.apache.cordova.file-transfer
Ps. Try plugin fileOpener2
https://build.phonegap.com/plugins/1117
Try this to open any kind of documents from URL using following steps:
install this plugin : cordova plugin add https://github.com/ti8m/DocumentHandler
use this code :
handleDocumentWithURL(function() { console.log('success'); }, function(error) { console.log('failure'); if (error == 53) { console.log('No app that handles this file type.'); } }, 'http://www.example.com/path/to/document.pdf');
It works for me both on Android and IOS. I used it for open images and PDF files.
Android : It opens files using system apps if available, otherwise it give an error, which you can handle.
IOS : It opens files in popup like view with Done button and Option button.
It doesn't show your docs URL.
Source is available here : https://github.com/ti8m/DocumentHandler
I wanted to know that is it possible to find version of an apk file that i've downloaded from the server?
i can retrieve the version of my app that is installed on device but i want to check installed app version on device with the downloaded apk file version.
You cannot easily get the version of the APK file you downloaded until you have installed it.
That being said, you can unzip the APK file (its just a zip file) and parse the AndroidManifest.xml to get the value of the version code.
Since the AndroidManifest.xml you extract is a binary file, you will need to write some code to parse it. It's not as simple as inspecting a plain text file.
Check out this post on how to parse the AndroidManifest which has been extracted from an APK.
How to parse the AndroidManifest.xml file inside an .apk package
PackageManager manager = this.getPackageManager();
PackageInfo info = manager.getPackageInfo("com.whatsapp", 0);
String packageName = info.packageName;
int versionCode = info.versionCode;
String versionName = info.versionName;
Replace com.whatsapp with your package name.
I'm looking for a way to automatically start the android Package Installer , after the browser finishes downloading an apk file . Any ideas on this ? Currently after the download is over the list of downloaded files is displayed and clicking on the downloaded apk launches the Package installer . (step which I'd like to automatize)
I've thought on launching the instalation manually with code like this :
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file:///sdcard/downloadedApk"),
"application/vnd.android.package-archive");
startActivity(intent);
,but I'm unable to find when the download completes to execute the snipet above.
You cannot "automatically start the android Package Installer , after the browser finishes downloading an apk file" -- sorry!
Uri uri = Uri.fromParts ( "package", "abc", null);
rtn = new Intent (Intent.ACTION_PACKAGE_ADDED, uri);