有无SD卡情况下,下载Apk,并安装
有无SD卡情况下,下载Apk,并安装
今天遇到一个问题,我的任务是下载一个apk,并安装,但是在无SD卡的情况下,包解析错误
我的思路是:
有SD卡就下载到SD卡,无SD卡就下载到getCacheDir()下面
但是因为这个目录是只能自己app使用,所以安装的时候就报了包解析错误的问题
我猜测原因是因为没有权限访问/data/data/com.xxx.xx/cache这个目录,所以需要设置个权限。
解决方法
File tFile = null;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
tFile = new File(FILE_PATH);
}else{
tFile = new File(mContext.getFilesDir(), URL_APP.hashCode()+".apk");
}然后下载完成的时候,一般都有一个rename的过程,这个时候需要做一下处理
private void renameFile(){
File file=new File(targetFile+DOWNLOAD_SUFFIX);
File target=new File(targetFile);
if(target.exists())
target.delete();
if(isInnerDir(file)){
FileOutputStream tFos = null;
FileInputStream tFis = null;
try {
tFos = FexApplication.getInstance().openFileOutput(target.getName(), Context.MODE_WORLD_READABLE);
tFis = FexApplication.getInstance().openFileInput(file.getName());
byte[] buffer = new byte[1024];
int length = 0;
while((length = tFis.read(buffer)) !=-1){
tFos.write(buffer, 0, length);
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try {
if(tFos!=null){
tFos.close();
}
if(tFis!=null){
tFis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}else{
try {
file.renameTo(target);
} catch (Exception e) {
e.printStackTrace();
}
}
}
private boolean isInnerDir(File tFile){
return tFile.getAbsolutePath().contains("data/data") && !tFile.getAbsolutePath().contains("Android");
}主要就是这个方法
openFileOutput(target.getName(), Context.MODE_WORLD_READABLE);
逻辑比较简单,直接看代码就行
相关推荐
OathKeeper 2020-04-22
mattraynor 2020-04-14
wzxxtt0 2020-02-15
学峰的学习笔记 2011-01-31
LiLiLiLaLa 2011-04-14
sgafdsg 2011-10-07
zhoujk00 2011-11-22
80266434 2017-07-27
IT精英联盟 2015-10-29
skywalker0 2015-07-26
85266438 2015-07-18
muyangzhe 2016-04-27
航帆远洋 2011-10-17
nankangren 2016-04-11
海绵宝宝的欧尼 2012-05-17
airgreen 2012-05-17
SouthWind0 2011-11-17
luoj 2011-09-30
xuguolibeyondboy 2011-08-17
Tom天天 2011-06-07
apk0 2011-05-16
xfcyhades 2011-03-08
zlsh00 2010-12-02
huzhenv 2010-09-17
yeaperyeo 2010-09-16
bible 2010-08-23
laisean 2009-11-03