PHPz2017-04-17 17:20:00
可以取得目前安裝應用的APK
public static String backupApplication(Context context,String packageName, String dest) {
if (packageName == null || packageName.length() == 0
|| dest == null || dest.length() == 0) {
return "illegal parameters";
}
PackageManager pm = context.getPackageManager();
PackageInfo pi = null;
try {
pi = pm.getPackageInfo(context.getPackageName(), 0);
} catch (NameNotFoundException e1) {
e1.printStackTrace();
}
// check file /data/app/appId-1.apk exists
Log.i("",""+pi.applicationInfo.sourceDir);
String apkPath = "/data/app/" + packageName + "-1.apk";
File apkFile = new File(apkPath);
if (!apkFile.exists()) {
apkFile=new File(pi.applicationInfo.sourceDir);
if(!apkFile.exists()){
return apkPath + " doesn't exist!";
}
}
FileInputStream in;
try {
in = new FileInputStream(apkFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
return e.getMessage();
}
// create dest folder if necessary
int i = dest.lastIndexOf('/');
if (i != -1) {
File dirs = new File(dest.substring(0, i));
dirs.mkdirs();
}
// do file copy operation
byte[] c = new byte[1024];
int slen;
FileOutputStream out = null;
try {
out = new FileOutputStream(dest);
while ((slen = in.read(c, 0, c.length)) != -1)
out.write(c, 0, slen);
} catch (IOException e) {
e.printStackTrace();
return e.getMessage();
} finally {
if (out != null)
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return "success";
}
天蓬老师2017-04-17 17:20:00
如果確定手機上的apk已經刪除了那麼只能靠工具來還原組裝一個apk出來(apk本身是一個程式和資源的壓縮包,安裝的過程就是一個解壓和註冊的過程)。
你可以試試apk share這個軟體,運行軟體後選擇app然後點backup就會還原apk檔案並放在你的sdcard裡。