不用Root权限获取已经安装的Apk安装包

    在安卓设备上安装的apk都会被保留一份在/data/app目录下,但是该目录对于普通用户来说只有可执行权限,是无法访问的。

    但是其子文件具有可读权限。

    

    意思也就说我们直接去查看/data/app这个目录是没办法的,但是通过写死文件的绝对路径是可以得到这个文件的。

    

/**
	* @Description 将app由data/app目录拷贝到sd卡下的指定目录中
	* @param appId 应用程序的ID号,如com.wondertek.jttxl
	* @param dest 需要将应用程序拷贝的目标位置
	* @date 2013-7-24 下午3:32:12
	*/
	private String backupApplication(String appId, String dest) {
		if (appId == null || appId.length() == 0 
				|| dest == null || dest.length() == 0) { 
			return "illegal parameters";
		}
		Util.Trace("[backupApplication] appId: " + appId + ", dest:" + dest);
		// check file /data/app/appId-1.apk exists
		String apkPath = "/data/app/" + appId + "-1.apk";
		File apkFile = new File(apkPath);
		if (apkFile.exists() == false) {
			return apkPath +  " doesn't exist!";
		}
		FileInputStream in = null;
		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();
			dirs = null;
		}
		// 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();
					return e.getMessage();
				}
			if (in != null) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
					return e.getMessage();
				}
			}
		}
		return "success";
	}

    目前存在一个问题,就是有的文件后缀名不是1,这个不知道除了人为判断有没有2或3之外有没有其他更好的办法

原文地址:https://www.cnblogs.com/riskyer/p/3292036.html