Android 单击下载按钮时没有响应
我的应用程序中有一个下载按钮,当按下该按钮时,应该从parse.com下载文件,我在那里存储了文件。但是,当点击按钮时,什么也没有发生,进度日志会在几秒钟内消失,并且不会执行下载 代码Android 单击下载按钮时没有响应,android,Android,我的应用程序中有一个下载按钮,当按下该按钮时,应该从parse.com下载文件,我在那里存储了文件。但是,当点击按钮时,什么也没有发生,进度日志会在几秒钟内消失,并且不会执行下载 代码 public class SingleItemView extends Activity { ProgressDialog mProgressDialog; String heading; String subHeading; String levelImg; String apiImg; String api
public class SingleItemView extends Activity
{
ProgressDialog mProgressDialog;
String heading;
String subHeading;
String levelImg;
String apiImg;
String apiText;
String descImgOne;
String descImgTwo;
String codeText;
ImageLoader imgloader = new ImageLoader(this);
private static String file_url;
private ProgressDialog pDialog;
ImageView my_image;
// Progress dialog type (0 - for Horizontal progress bar)
public static final int progress_bar_type = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
Intent i = getIntent();
heading= i.getStringExtra("heading");
subHeading = i.getStringExtra("subheading");
levelImg = i.getStringExtra("levelimg");
apiImg = i.getStringExtra("apiimg");
apiText = i.getStringExtra("apitext");
descImgOne = i.getStringExtra("descimgone");
descImgTwo = i.getStringExtra("descimgtwo");
codeText = i.getStringExtra("codetext");
file_url = i.getStringExtra("download");
TextView headingtxt = (TextView)findViewById(R.id.singleitemheading);
TextView subheadingtxt = (TextView)findViewById(R.id.singleitemsubheading);
TextView apitxt = (TextView)findViewById(R.id.singleitemviewapitext);
TextView codetxt = (TextView) findViewById(R.id.singleitemviewcodetext);
ImageView level =(ImageView) findViewById(R.id.levelimg);
ImageView api =(ImageView) findViewById(R.id.singleitemviewapiimg);
ImageView descone =(ImageView) findViewById(R.id.descriptionimgone);
ImageView desctwo =(ImageView) findViewById(R.id.descriptionimgtwo);
Button downloadbtn = (Button) findViewById(R.id.singleitemviewButton);
headingtxt.setText(heading);
subheadingtxt.setText(subHeading);
apitxt.setText(apiText);
codetxt.setText(codeText);
imgloader.DisplayImage(levelImg, level);
imgloader.DisplayImage(apiImg, api);
imgloader.DisplayImage(descImgOne, descone);
imgloader.DisplayImage(descImgTwo, desctwo);
downloadbtn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
String downurl = file_url;
new DownloadFileFromURL().execute(downurl);
}
});
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case progress_bar_type: // we set this to 0
pDialog = new ProgressDialog(this);
pDialog.setMessage("Downloading file. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(true);
pDialog.show();
return pDialog;
default:
return null;
}
}
class DownloadFileFromURL extends AsyncTask<String, String, String>
{
/**
* Before starting background thread
* Show Progress Bar Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(progress_bar_type);
}
/**
* 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();
// this will be useful so that you can show a tipical 0-100% progress bar
int lenghtOfFile = conection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(), 8192);
// Output stream
OutputStream output = new FileOutputStream("/sdcard/download/downloadedfile.png");
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
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) {
//e.printStackTrace();
Log.e("Error: ", e.getMessage());
}
return null;
}
/**
* Updating progress bar
* */
protected void onProgressUpdate(String... progress) {
// setting progress percentage
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
* After completing background task
* Dismiss the progress dialog
* **/
@Override
protected void onPostExecute(String downurl) {
// dismiss the dialog after the file was downloaded
dismissDialog(progress_bar_type);
}
}
}
公共类SingleItemView扩展活动
{
进程对话框;
字符串标题;
弦副标题;
字符串级别img;
字符串apiImg;
字符串和文本;
字符串描述;
字符串descImgTwo;
字符串代码文本;
ImageLoader imgloader=新的ImageLoader(此);
私有静态字符串文件\u url;
私人对话;
图像查看我的图像;
//进度对话框类型(0-用于水平进度条)
公共静态最终整数进度条类型=0;
@凌驾
创建时受保护的void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.singleitemview);
Intent i=getIntent();
标题=i.getStringExtra(“标题”);
子目=i.getStringExtra(“子目”);
levelImg=i.getStringExtra(“levelImg”);
apiImg=i.getStringExtra(“apiImg”);
apiText=i.getStringExtra(“apiText”);
descImgOne=i.getStringExtra(“descImgOne”);
descImgTwo=i.getStringExtra(“descImgTwo”);
codeText=i.getStringExtra(“codeText”);
file_url=i.getStringExtra(“下载”);
TextView headingText=(TextView)findViewById(R.id.singleitemheading);
TextView子目TXT=(TextView)findViewById(R.id.singleitemsubheading);
TextView apitxt=(TextView)findViewById(R.id.singleItemViewApitText);
TextView codetxt=(TextView)findViewById(R.id.singleitemviewcodetext);
ImageView级别=(ImageView)findViewById(R.id.levelimg);
ImageView api=(ImageView)findViewById(R.id.singleitemviewapiimg);
ImageView descone=(ImageView)findViewById(R.id.DescriptionIngOne);
ImageView desctwo=(ImageView)findViewById(R.id.descriptionimgtwo);
按钮下载BTN=(按钮)findViewById(R.id.singleitemviewButton);
HeadingText.setText(标题);
子目TXT.setText(子目);
apittxt.setText(apiText);
codetxt.setText(codeText);
imgloader.DisplayImage(levelImg,level);
显示图像(apiImg,api);
显示图像(descImgOne,descone);
imgloader.DisplayImage(descImgTwo,desctwo);
下载btn.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
字符串downurl=文件\ url;
新建DownloadFileFromURL().execute(downurl);
}
});
}
@凌驾
受保护的对话框onCreateDialog(int id){
开关(id){
案例进度条类型://我们将其设置为0
pDialog=新建进度对话框(此对话框);
setMessage(“正在下载文件,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_水平);
pDialog.setCancelable(真);
pDialog.show();
返回pDialog;
违约:
返回null;
}
}
类DownloadFileFromURL扩展异步任务
{
/**
*在启动后台线程之前
*显示进度条对话框
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
显示对话框(进度条类型);
}
/**
*在后台线程中下载文件
* */
@凌驾
受保护的字符串doInBackground(字符串…f_url){
整数计数;
试一试{
URL=新URL(f_URL[0]);
URLConnection conconnection=url.openConnection();
conconnect.connect();
//这将非常有用,以便您可以显示典型的0-100%进度条
int lenghtOfFile=conconnect.getContentLength();
//下载该文件
InputStream输入=新的BufferedInputStream(url.openStream(),8192);
//输出流
OutputStream output=新文件OutputStream(“/sdcard/download/downloadedfile.png”);
字节数据[]=新字节[1024];
长总计=0;
而((计数=输入。读取(数据))!=-1){
总数+=计数;
//发布进度。。。。
//在此之后,将调用onProgressUpdate
出版进度(“+(int)((总计*100)/长度文档));
//将数据写入文件
输出.写入(数据,0,计数);
}
//冲洗输出
output.flush();
//合流
output.close();
input.close();
}捕获(例外e){
//e、 printStackTrace();
Log.e(“错误:,e.getMessage());
}
返回null;
}
/**
*更新进度条
* */
受保护的void onProgressUpdate(字符串…进度){
//设置进度百分比
pDialog.setProgress(Integer.parseInt(progress[0]));
}
/**
*完成后台任务后
*关闭进度对话框
* **/
@凌驾
受保护的void onPostExecute(字符串downurl){
//下载文件后关闭对话框
解雇对话框(进度条类型);
}
}
}
可能有一些日志?@AntonShkurenko不,没有什么需要调试的,也放一些日志。可能文件是空的,所以它不下载任何内容?在将其传递给AsyncTask时,是否仔细检查了downurl
的值?那么lenghtOfFile
呢?您是否具有写入外部存储的权限?您确定存在有效值吗?