[英]Downloading an apk, with not such file or directory error..
我正在創建一個從網絡服務器下載apk文件然后安裝的應用程序,但是我遇到了問題,當我單擊“下載”時,進度條會比較,但不會更改,在Android Monitor中會比較:
我正在Android 7.1.1上使用avd,然后嘗試使用棉花糖。 這是代碼:
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();
}
Android 6.0之后的Android Need運行時權限請參閱https://developer.android.com/training/permissions/requesting.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.