我正在处理一个项目,因为我在点击ListView
并同时显示horizontal progress bar
时正在下载.pdf文件。但是,它停在1%而不是继续前进,文件也没有下载..
请检查代码..我错过了什么?
CODE
public class Downloads_screen extends Activity
{
ProgressDialog mProgressDialog;
String mPdf_links;
String mlist;
String img_path;
@Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.downloads_screen);
Intent i = getIntent();
mlist = i.getExtras().getString("TITLE");
mPdf_links = i.getExtras().getString("PDFPATH");
img_path = i.getExtras().getString("IMGPATH");
Log.v("Download Page", "Title: "+mlist);
Log.v("Download Page", "Pdf Path: "+mPdf_links);
RelativeLayout backbutton = (RelativeLayout) findViewById(R.id.download_screen_backbutton);
backbutton.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
finish();
}
});
Boolean isFilePresent;
String filename = mPdf_links.substring(mPdf_links.lastIndexOf('/') + 1, mPdf_links.length());
File downloadfile = new File("/sdcard/CONSTRUCTION/" + filename);
if (downloadfile.exists()) {
isFilePresent = true;
} else {
isFilePresent= false;
}
System.out.println(filename + ":" + isFilePresent);
MyDownload_adapter adapter = new MyDownload_adapter(Downloads_screen.this, mlist, img_path, isFilePresent);
ListView downloadlist = (ListView) findViewById(R.id.download_screen_listView1);
downloadlist.setAdapter(adapter);
downloadlist.setOnItemClickListener(new OnItemClickListener() {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
// instantiate it within the onCreate method
String filename = mPdf_links.substring(
mPdf_links.lastIndexOf('/') + 1,
mPdf_links.length());
System.out.println("FileName:" + filename);
File downloadfile = new File("/sdcard/CONSTRUCTION/" + filename);
if (!downloadfile.exists()) {
mProgressDialog = new ProgressDialog(Downloads_screen.this);
mProgressDialog.setMessage("Downloading..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setProgress(0);
mProgressDialog.incrementProgressBy(1);
mProgressDialog.setMax(100);
mProgressDialog
.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// execute this when the downloader must be fired
DownloadFile downloadFile = new DownloadFile();
System.out.println(mPdf_links);
downloadFile.execute(mPdf_links);
} else {
File file = new File(Environment
.getExternalStorageDirectory().getAbsolutePath()
+ "/CONSTRUCTION/" + filename);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
}
});
}
private class DownloadFile extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
try {
URL url = new URL(sUrl[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100%
// progress bar
int fileLength = connection.getContentLength();
System.out.println("File length:" + fileLength);
System.out.println("TEST here in Downloadfile");
// download the file
File wallpaperDirectory = new File("/sdcard/CONSTRUCTION/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
String filename = sUrl[0].substring(
sUrl[0].lastIndexOf('/') + 1, sUrl[0].length());
System.out.println("FileName:" + filename);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(
"/sdcard/CONSTRUCTION/" + filename);
byte data[] = new byte[1024];
long total = 0;
int count;
while ((count = input.read(data)) != -1) {
total += count;
System.out.println("Total downloaded" + total + " count:"
+ count);
// publishing the progress....
publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
mProgressDialog.dismiss();
File file = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/CONSTRUCTION/" + filename);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
} catch (Exception e) {
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
mProgressDialog.setProgress(progress[0]);
}
}
}
答案 0 :(得分:0)
我上面的代码工作正常......
问题是我得到的文件的名称是不正确的,即它有特殊字符和空格,下载者无法得到它,因此下载没有启动。
告诉您的web_dept人员将文件重命名为正确的格式。或者你必须使用字符串操作来正确地重命名文件。