在开发一个允许用户检查新的应用程序更新的功能时,我卡了好几天(我使用本地服务器作为分发点)。问题在于下载进度似乎完美运行,但我找不到已下载的文件在我的手机上的任何位置(我的手机没有SD卡/外部存储器)。以下是我目前的进展。
class DownloadFileFromURL extends AsyncTask<String, String, String> {
ProgressDialog pd;
String path = getFilesDir() + "/myapp.apk";
@Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(DashboardActivity.this);
pd.setTitle("Processing...");
pd.setMessage("Please wait.");
pd.setMax(100);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setCancelable(true);
//pd.setIndeterminate(true);
pd.show();
}
/**
* Downloading file in background thread
* */
@Override
protected String doInBackground(String... f_url) {
int count;
try {
URL url = new URL(f_url[0]);
URLConnection conection = url.openConnection();
conection.connect();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(path);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100) / lenghtOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
}
return path;
}
protected void onProgressUpdate(String... progress) {
pd.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String file_url) {
// dismiss the dialog after the file was downloaded
if (pd!=null) {
pd.dismiss();
}
// i am going to run the file after download finished
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(file_url)), "application/vnd.android.package-archive" );
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Log.d("Lofting", "About to install new .apk");
getApplicationContext().startActivity(i);
}
}
当进度对话框达到100%并消失后,我找不到文件。 我认为这就是应用程序无法继续安装下载的apk的原因。
我错过了一些代码吗?
file_url
的值是什么? - Rohit5k2