【发布时间】:2017-05-06 19:24:58
【问题描述】:
我正在尝试在 Google Play 商店之外下载并安装更新到我的 android。 使用本教程作为指南 (http://simpledeveloper.com/how-to-update-android-apk-outside-the-playstore/) 我在我的应用程序中插入了代码,以便从我的谷歌驱动器下载我的应用程序的 apk 文件(包含更新)。
应用只下载文件的一部分(可能是前 14KB 到 100KB)。
我做错了什么?
这是我的代码:
public class ApkUpdateTask extends AsyncTask <String,Void,Void>{
private Context context;
public void setContext(Context contextf){
context = contextf;
}
@Override
protected Void doInBackground(String... arg0) {
try {
URL url = new URL(arg0[0]);
HttpURLConnection c = (HttpURLConnection) url.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
String PATH = "/storage/emulated/0/Download/";
//String PATH = Environment.getExternalStorageDirectory().getPath();
File file = new File(PATH);
//file.mkdirs();
File outputFile = new File(file, "sonandso.apk");
if(outputFile.exists()){
outputFile.delete();
}
FileOutputStream fos = new FileOutputStream(outputFile);
InputStream is = c.getInputStream();
byte[] buffer = new byte[1024];
int len1;
while ((len1 = is.read(buffer)) != -1) {
fos.write(buffer, 0, len1);
}
fos.close();
is.close();
Intent intent = new Intent(Intent.ACTION_VIEW);
//intent.setDataAndType(Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath())), "blahblah.apk");
intent.setDataAndType(Uri.fromFile(new File("/storage/emulated/0/Download/")), "blahblah.apk");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // without this flag android returned a intent error!
context.startActivity(intent);
} catch (Exception e) {
Log.e("UpdateAPP", "Update error! " + e.getMessage());
}
return null;
}
}
注意事项:
- 我制作了已签名和未签名的 apk。两者都无法下载完整的应用。
- 我将 apk 的 api 级别设置为与设备相同的 api 级别(在 gradle 构建文件中。我使用的是 Android Studio)。 Android版本是5.1.1(肯定是项目原因)。
- 我正在从主片段调用上面列出的代码(我放入一个单独的类文件中)。这应该从主要活动本身调用吗??
- 我正在尝试写入内部存储器而不是外部 SD 卡。这可能是问题吗?如果是这样 - 我如何将其写入内部存储器?
- 我也尝试使用“Environment.getExternalStorageDirectory().getPath()”作为路径,但也没有用。有关问题,请参阅 #4。
【问题讨论】:
标签: android android-install-apk android-internal-storage