textview提示:下载失败,已经手动申请权限,xml中也添加了三个相关权限申请
package com.example.handler;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class DownloadActivity extends AppCompatActivity {
public static final int DOWNLOAD_MESSAGE_CODE = 100001;
public static final String DONWLOAD_URL = "http://downlaod.sj.qq.com/upload/connAssistantDownload/upload/MobileAssistant_1.apk";
private static final int MY_PERMISSION_REQUEST_CODE = 10000;
private Handler myhandler;
public TextView textView;
public static final int DOWNLOAD_MESSAGE_FAILCODE = 100002;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
/*完成功能:点击按钮,下载网络数据,在progressbar中显示下载进度
*(1) 主线程-->start
*(2) 点击按钮
*(3)发起下载
*(4) 开启子线程下载
*(5)子线程下载过程中通知主线程-->主线程更新进度条*/
final ProgressBar progressBar = findViewById(R.id.progressbar_donwload);
textView = findViewById(R.id.tv_download);
findViewById(R.id.bt_donwload).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//开辟子线程,进行下载
new Thread(new Runnable() {
@Override
public void run() {
download(DONWLOAD_URL);
}
}).start();
}
});
myhandler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what)//对传来的信息作一个判断,并作出相关响应
{
case DOWNLOAD_MESSAGE_CODE:
progressBar.setProgress( msg.arg1);
if ( msg.arg1 == 100) {
textView.setText(msg.obj+"下载完成");
} else
textView.setText("下载中……");
break;
case DOWNLOAD_MESSAGE_FAILCODE:
textView.setText("下载失败");
break;
}
}
};
}
private void download(String downloadUrl) {
//第一步:检查是否有相的权限
boolean isAllGranted=checkPermissionAllGranted(
new String[]{
Manifest.permission.INTERNET,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
});
if(isAllGranted){
doDownload(downloadUrl);//已经得到所有授权的话,直接下载
return;
}
//第2步:请求权限,一次性请求多个权限,如果其它权限已经授予的将会自动忽略掉
ActivityCompat.requestPermissions(
this,
new String[]{
Manifest.permission.INTERNET,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
},
MY_PERMISSION_REQUEST_CODE
);
}
/*检查是否拥有指定的所有权限*/
private boolean checkPermissionAllGranted(String[] permissions) {
for(String permission:permissions){
if(ContextCompat.checkSelfPermission(this,permission)!= PackageManager.PERMISSION_GRANTED)
{//只要有一个权限没有被授予,直接返回false
return false;
}
}
return true;
}
/*第3步:申请权限结果返回处理*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(requestCode==MY_PERMISSION_REQUEST_CODE){
boolean isAllGranted=true;
//判断是否所有权限都已经授权了
for(int grant:grantResults){
if(grant!=PackageManager.PERMISSION_GRANTED){
isAllGranted=false;
break;
}
}
if(isAllGranted){
//如果所有的权限都授予了,则执行下载任务
doDownload(DONWLOAD_URL);
}
}
}
private void doDownload(String appUrl) {
try {
/*
* (1)获取网络下载文件总长度
* (2)获取存储文件目录地址(手机等移动设备)
*(3)用输入输出流把网络下载数据写入本地文件
*(4)同时统计下载数据长度,通过handler.sendmessage传给主线程,告知下载进度
* */
URL url = new URL(appUrl);//根据网络地址参数appUrl新建URL对象
URLConnection urlConnection = url.openConnection();//建立url连接
InputStream inputStream = urlConnection.getInputStream();//新建输入流读取网络数据
int contentLength = urlConnection.getContentLength();
String downloadFolderName = Environment.getExternalStorageDirectory()
+ File.separator + "imooc" + File.separator;
File file = new File(downloadFolderName);//新建文件夹对象
if (!file.exists()) {
file.mkdir();//创建文件夹
}
String fileName = downloadFolderName + "imooc.apk";//完整的对象目录+文件名
File apkFile = new File(fileName);//新建需要下载的文件对象
if(apkFile.exists())//如果该文件已经存在,删除掉
{
apkFile.delete();
}
int downloadSize = 0;
byte[] bytes = new byte[1024];
int length = 0;
OutputStream outputStream = new FileOutputStream(fileName);//新建输出流,用以把网络下载数据写入本地文件
while ((length = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, length);
// outputStream.flush();
downloadSize += length;
/*updateUI*/
Message message = Message.obtain();
message.obj = fileName;//传递文件名及所在位置
message.arg1 = downloadSize * 100 / contentLength;//进度条数值
message.what = DOWNLOAD_MESSAGE_CODE;
myhandler.sendMessage(message);
}
inputStream.close();
outputStream.close();
} catch (MalformedURLException e) {
/*updateUI*/
notifyDownloadFailed();//通知主线程,下载失败
e.printStackTrace();
} catch (IOException e) {
notifyDownloadFailed();
e.printStackTrace();
}
}
private void notifyDownloadFailed() {
Message message = Message.obtain();
message.what = DOWNLOAD_MESSAGE_FAILCODE;//设置下载失败代码10002
myhandler.sendMessage(message);
}
}
正在回答
你的download拼写错了
修改成正确的网址,真机运行依然提示:下载失败,错误提示:
- 参与学习 人
- 提交作业 307 份
- 解答问题 1613 个
本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星