我正在创建一个从网络服务器下载apk文件然后安装它的应用程序,但我遇到了问题,当我点击下载时,进度条会比较但不会改变,并且在Android中Monitor比较一下:
我正在使用带有android 7.1.1的avd,我将尝试使用marshmallow。 这是代码:
try {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String sdPath = Environment.getExternalStorageDirectory() + "/";
mSavePath = sdPath + "updateDownload";
File dir = new File(mSavePath);
if (!dir.exists())
if (dir.mkdir())
Log.d(TAG, "mkdir success");
else
Log.d(TAG, "mkdir failed!");
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.connect();
InputStream is = conn.getInputStream();
int length = conn.getContentLength();
File apkFile = new File(mSavePath, version_name);
FileOutputStream fos = new FileOutputStream(apkFile);
int count = 0;
byte[] buffer = new byte[1024];
while (!isCancel) {
int numread = is.read(buffer);
count += numread;
mProgress = (int) ((float) (count / length) * 100);
mUpdateProgressHandler.sendEmptyMessage(DOWNLOADING);
if (numread < 0) {
mUpdateProgressHandler.sendEmptyMessage(DOWNDLOAD_FINISHED);
break;
}
fos.write(buffer, 0, numread);
}
fos.close();
is.close();
} else {
Log.d(TAG, " Media not mounted");
}
} catch (Exception e) {
e.printStackTrace();
}
答案 0 :(得分:1)
Android在Android 6.0之后需要运行时权限 参考https://developer.android.com/training/permissions/requesting.html