我使用下面的代碼從互聯網下載文件。使用此代碼我有一個進度對話框文件傳輸到SD的進度條
private void startDownload() {
String url = tv.getText().toString();
new DownloadFileAsync().execute(url);
}
class DownloadFileAsync extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
showDialog(DIALOG_DOWNLOAD_PROGRESS);
}
@Override
protected String doInBackground(String... aurl) {
int count;
try {
URL url = new URL(aurl[0]);
URLConnection conexion = url.openConnection();
conexion.connect();
int lenghtOfFile = conexion.getContentLength();
Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(
"data/data/com.xxxxxxxx.android/app_p_pic/"
+ "blabla"
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
publishProgress("" + (int) ((total * 100)/lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
e.printStackTrace();
}
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);
}
}
}
是有使用上面的代碼傳送從文件的方式說內部SD?在momment我用下面的代碼,但IV一直無法得到進度與進度條工作
protected void ontrans() {
String title = tv.getText().toString();
try {
myInput = new FileInputStream(title);
File directory = new File(Environment.getExternalStorageDirectory()
+ "/android_files/data/secure");
if (!directory.exists()) {
directory.mkdirs();
}
OutputStream myOutput = new FileOutputStream(
Environment.getExternalStorageDirectory()
+ "/android_files/data/secure/" + chosenFile);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
Toast.makeText(File_exActivity.this, "file move Succesfully!",
Toast.LENGTH_SHORT).show();
showDialog(11);
} catch (FileNotFoundException e) {
Toast.makeText(File_exActivity.this, "no file found!",
Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
Toast.makeText(File_exActivity.this, "move unsuccesfull!",
Toast.LENGTH_LONG).show();
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我已經得到了一個進度條,在第一個代碼中工作,代碼從互聯網下載,第二個代碼我用來從SD轉移到內部,到SDD ..我需要的是第一個代碼能夠在顯示進度的同時轉移文件或顯示進度的第二個代碼。 – steo 2012-02-12 09:43:53