我正在尝试使用水平进度条下载图像,然后将其存储在SD卡中的文件中,代码如下:
package com.erc.library;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Splashscreen extends Activity {
ProgressBar pd;
String a="";
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splashscreen);
pd=(ProgressBar)findViewById(R.id.progressBar1);
pd.setMax(100);
new ProgressTask().execute();
}
private class ProgressTask extends AsyncTask <String,String,String>
{
@Override
protected void onPreExecute()
{
pd.setIndeterminate(false);
pd.setMax(100);
Toast.makeText(getBaseContext(),a,Toast.LENGTH_LONG).show();
}
@Override
protected void onProgressUpdate(String... progress) {
pd.setProgress(Integer.parseInt(progress[0]));
}
protected void onPostExecute(Void result)
{
Toast.makeText(getBaseContext(),"done",Toast.LENGTH_LONG).show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
///////////////////////////////////////////////////////////////////////////////////////////////////////
try
{
URL url = new URL("http://mylink/myimage.png");
URLConnection ucon = url.openConnection();
// ucon.setReadTimeout(15000);
// ucon.setConnectTimeout(30000);
InputStream is = ucon.getInputStream();
int lenghtOfFile = ucon.getContentLength();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath()+"/library";
String fileName = "test.png";
// Not sure if the / is on the path or not
File file = new File(baseDir + File.separator + fileName);
// File file = new File(this.getDir(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Sayegh library", Context.MODE_PRIVATE) + "/yourfile.png");
if (file.exists())
{
file.delete();
}
file.createNewFile();
FileOutputStream outStream = new FileOutputStream(file);
byte[] buff = new byte[5 * 1024];
int len;
byte data[] = new byte[1024];
long total = 0;
while ((len = inStream.read(buff)) != -1)
{
total += len;
// publishing the progress....
// After this onProgressUpdate will be called
// pd.setProgress((int) ((total * 100) / lenghtOfFile));
// publishProgress("" + (int) ((total * 100) / lenghtOfFile));
outStream.write(buff, 0, len);
}
outStream.flush();
outStream.close();
inStream.close();
}
catch (Exception e)
{
a=e.toString();
}
///////////////////////////////////////////////////////////////////////////////////////////////////////
return null;
}
}
}
进度条没有显示任何进度,图片没有下载,请帮忙吗?
答案 0 :(得分:0)
看起来您需要设置权限。
如果没有帮助,请阅读adb logcat
输出并在此处发布相关部分。