我想改變我是從互聯網上下載使用XML文件的幫助文件的文件名:Android:如何更改下載文件的文件名?
<?xml version="1.0" encoding="ISO-8859-1" ?>
<Phonebook>
<PhonebookEntry>
<firstname>Michael</firstname>
<lastname>De Leon</lastname>
<Address>5, Cat Street</Address>
<FileURL>http://www.technobuzz.net/wp-content/uploads/2009/09/Android-Emulator.jpg</FileURL>
</PhonebookEntry>
<PhonebookEntry>
<firstname>John</firstname>
<lastname>Smith</lastname>
<Address>6, Dog Street</Address>
<FileURL>http://www.cellphonehits.net/uploads/2008/10/android_openmoko.jpg</FileURL>
</PhonebookEntry>
<PhonebookEntry>
<firstname>Jember</firstname>
<lastname>Dowry</lastname>
<Address>7, Monkey Street</Address>
<FileURL>http://www.techdigest.tv/android.jpg</FileURL>
</PhonebookEntry>
</Phonebook>
我的程序有以下代碼:
package com.example.parsingxml;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
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.util.Log;
import android.widget.TextView;
public class ParsingXML extends Activity {
public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;
public String FileName = "";
public String FileURL = "";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Create a new TextView to display the parsingresult later. */
TextView tv = new TextView(this);
tv.setText("This is the parsing program...");
try {
/* Create a URL we want to load some xml-data from. */
URL url = new URL("http://cloud.eacomm.com/jm/sampleXML.xml");
url.openConnection();
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
List<ParsedExampleDataSet> parsedExampleDataSet = myExampleHandler.getParsedData();
/* Set the result to be displayed in our GUI. */
//tv.setText(parsedExampleDataSet.toString());
Iterator i;
i = parsedExampleDataSet.iterator();
ParsedExampleDataSet dataItem;
while(i.hasNext()){
dataItem = (ParsedExampleDataSet) i.next();
tv.append("\n" + dataItem.getfirstname());
tv.append("\n" + dataItem.getFileURL());
tv.append("\n" + dataItem.getAddress());
this.FileName = dataItem.getfirstname() + ".jpg";
this.FileURL = dataItem.getFileURL();
startDownload();
}
} catch (Exception e) {
/* Display any Error to the GUI. */
tv.setText("Error: " + e.getMessage());
}
/* Display the TextView. */
this.setContentView(tv);
}
private void startDownload(){
new DownloadFileAsync().execute(FileURL);
}
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Downloading file..");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.show();
return mProgressDialog;
default:
return null;
}
}
class DownloadFileAsync extends AsyncTask<String, String, String>{
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
try {
File root = Environment.getExternalStorageDirectory();
URL u = new URL(FileURL);
HttpURLConnection c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setDoOutput(true);
c.connect();
int lenghtOfFile = c.getContentLength();
FileOutputStream f = new FileOutputStream(new File(root + "/download/", FileName));
InputStream in = c.getInputStream();
byte[] buffer = new byte[1024];
int len1 = 0;
long total = 0;
while ((len1 = in.read(buffer)) > 0) {
total += len1; //total = total + len1
publishProgress("" + (int)((total*100)/lenghtOfFile));
f.write(buffer, 0, len1);
}
f.close();
} catch (Exception e) {
Log.d("Downloader", e.getMessage());
}
return null;
}
protected void onProgressUpdate(String... progress) {
Log.d("ANDRO_ASYNC",progress[0]);
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}
@Override
protected void onPostExecute(String unused) {
dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
}
}
}
,你可以看,下載文件的文件名被認爲是.jpg,但是當我查看我的SD卡/ download /時,唯一下載的文件是Jember.jpg。我相信它只是覆蓋其他兩個文件。我怎樣才能輸出Michael.jpg,John.jpg和Jember.jpg?
哇!你的代碼有效!謝啦! :) – Kris 2011-05-06 09:24:32