Reputation: 1779
I am writing an app which downloads an *.apk from a url and then tries to install it. It appears I am running into Permission denied error when trying to get PackageManager to install it. I want set the permission of the file to readable from java code. How do you do this.
Here is how I am reading the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(PATH + fileName);
byte data[] = new byte[1024];
int count;
while((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
Upvotes: 2
Views: 11767
Reputation: 1556
I haven't tried exactly what is being asked here, but this will change a files permissions in any version of android:
exec("chmod 0777 " + fileOfInterest.getAbsolutePath()); //whatever permissions you require
private void exec(String command) {
Runtime runtime = Runtime.getRuntime();
Process process;
try {
process = runtime.exec(command);
try {
String str;
process.waitFor();
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((str = stdError.readLine()) != null) {
Log.e("Exec",str);
mErrOcc = true; //I use this elsewhere to determine if an error was encountered
}
process.getInputStream().close();
process.getOutputStream().close();
process.getErrorStream().close();
} catch (InterruptedException e) {
mErrOcc = true;
}
} catch (IOException e1) {
mErrOcc = true;
}
}
This is like what Shmuel was suggesting, but more complete and btw, what he suggested does work in 1.5 and higher, not 2.2 and higher.
Upvotes: 0
Reputation: 36
if you want to change file permissions on android 2.2 and higher you can use this:
Runtime.getRuntime().exec("chmod 777 " + PATH + fileName);
in android 3 you can do it also without using a native code workaround:
new java.io.File(PATH + fileName).setReadable(true, false);
Upvotes: 2
Reputation: 74790
You can't install .apk files directly using PackageManager
. Only system applications can do this.
But you can ask system to install application using standard installation workflow. Here is an example:
Intent intent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(new File(pathToApk));
intent.setDataAndType(uri, "application/vnd.android.package-archive");
startActivity(intent);
This all assuming you actually succeeded downloading the .apk
. But if you failed at that step, you should check your WRITE_EXTERNAL_STORAGE permission. And also check that your sdcard is not shared via USB (if so, your application will not have write permission to sdcard).
Upvotes: 2