我必须从android手机中的sdcard上传文件,我尝试在textview中显示文件名,其中列表是在进度栏中上传的。我添加了我遵循的代码。但结果并不是我的预期。问题场景是该文件夹包含20个音乐文件,我必须在进度条中逐个显示1到20个文件名,但它只显示进度条中的第20个文件名。请帮助我提出我的谦卑要求。在此先感谢。
UploadFileSequnce.Java
public class UploadFileSequnce extends Activity {
ProgressBar pb;
Dialog dialog;
int downloadedSize = 0;
int totalSize = 0;
TextView cur_val;
String fileName = "";
String dwnload_file_path = "http://cdn.shopify.com/s/files/1/0193/0692/products/mocg2smm0206.jpeg";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b = (Button) findViewById(R.id.b1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// showProgress(dwnload_file_path);
showFileProgress();
/*
* new Thread(new Runnable() { public void run() {
* //downloadFile(); //loadFileNames(); } }).start();
*/
}
});
}
void downloadFile() {
try {
URL url = new URL(dwnload_file_path);
HttpURLConnection urlConnection = (HttpURLConnection) url
.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
// connect
urlConnection.connect();
// set the path where we want to save the file
File SDCardRoot = Environment.getExternalStorageDirectory();
// create a new file, to save the downloaded file
File file = new File(SDCardRoot, "downloaded_file.png");
FileOutputStream fileOutput = new FileOutputStream(file);
// Stream used for reading the data from the internet
InputStream inputStream = urlConnection.getInputStream();
// this is the total size of the file which we are downloading
totalSize = urlConnection.getContentLength();
runOnUiThread(new Runnable() {
public void run() {
pb.setMax(totalSize);
}
});
// create a buffer...
byte[] buffer = new byte[1024];
int bufferLength = 0;
while ((bufferLength = inputStream.read(buffer)) > 0) {
fileOutput.write(buffer, 0, bufferLength);
downloadedSize += bufferLength;
// update the progressbar //
runOnUiThread(new Runnable() {
public void run() {
pb.setProgress(downloadedSize);
float per = ((float) downloadedSize / totalSize) * 100;
cur_val.setText("Downloaded " + downloadedSize
+ "KB / " + totalSize + "KB (" + (int) per
+ "%)");
}
});
}
// close the output stream when complete //
fileOutput.close();
runOnUiThread(new Runnable() {
public void run() {
// pb.dismiss(); // if you want close it..
}
});
} catch (final MalformedURLException e) {
showError("Error : MalformedURLException " + e);
e.printStackTrace();
} catch (final IOException e) {
showError("Error : IOException " + e);
e.printStackTrace();
} catch (final Exception e) {
showError("Error : Please check your internet connection " + e);
}
}
void showError(final String err) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(UploadFileSequnce.this, err, Toast.LENGTH_LONG)
.show();
}
});
}
void showProgress(String file_path) {
dialog = new Dialog(UploadFileSequnce.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.myprogressdialog);
dialog.setTitle("Download Progress");
TextView text = (TextView) dialog.findViewById(R.id.tv1);
text.setText("Downloading file from ... " + file_path);
cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv);
cur_val.setText("Starting download...");
dialog.show();
pb = (ProgressBar) dialog.findViewById(R.id.progress_bar);
pb.setProgress(0);
pb.setProgressDrawable(getResources().getDrawable(
R.drawable.green_progress));
}
void showFileProgress() {
dialog = new Dialog(UploadFileSequnce.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.myprogressdialog);
dialog.setTitle("File Progress");
// loadFileNames();
TextView text = (TextView) dialog.findViewById(R.id.tv1);
text.setText("Upload file from ... " + loadFileNames());
cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv);
cur_val.setText("Starting Upload...");
dialog.show();
pb = (ProgressBar) dialog.findViewById(R.id.progress_bar);
pb.setProgress(0);
pb.setProgressDrawable(getResources().getDrawable(
R.drawable.green_progress));
}
String loadFileNames() {
// File sdCardRoot = Environment.getExternalStorageDirectory();
// File yourDir = new File(sdCardRoot, "Download");
String path = "storage/sdcard1/music mohan";
File ff = new File(path);
if (ff != null) {
File yourDir = new File(ff, "mohan hits");
for (File f : yourDir.listFiles()) {
System.out.println("Filename:" + f.getName());
if (f.isDirectory())
fileName = f.getName();
}
}
return fileName;
}
}
答案 0 :(得分:0)
更新代码:
loadFileNames();
void showFileProgress() {
dialog = new Dialog(UploadFileSequnce.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.myprogressdialog);
dialog.setTitle("File Progress");
TextView text = (TextView) dialog.findViewById(R.id.tv1);
text.setText("Upload file from ... " + fileName);
cur_val = (TextView) dialog.findViewById(R.id.cur_pg_tv);
cur_val.setText("Starting Upload...");
dialog.show();
pb = (ProgressBar) dialog.findViewById(R.id.progress_bar);
pb.setProgress(0);
pb.setProgressDrawable(getResources().getDrawable(
R.drawable.green_progress));
}
String loadFileNames() {
// File sdCardRoot = Environment.getExternalStorageDirectory();
// File yourDir = new File(sdCardRoot, "Download");
String path = "storage/sdcard1/music mohan";
File ff = new File(path);
if (ff != null) {
File yourDir = new File(ff, "mohan hits");
for (File f : yourDir.listFiles()) {
System.out.println("Filename:" + f.getName());
if (f.isDirectory())
fileName = f.getName();
}
showFileProgress();
}
return fileName;
}