Come lei ha detto che si sta utilizzando InputStreamVolleyRequest
, spero che tu abbia scritto il seguente codice o qualcosa di simile, così:
@Override
public void onResponse(byte[] response) {
HashMap<String, Object> map = new HashMap<String, Object>();
try {
if (response!=null) {
String content =request.responseHeaders.get("Content-Disposition")
.toString();
StringTokenizer st = new StringTokenizer(content, "=");
String[] arrTag = st.toArray();
String filename = arrTag[1];
filename = filename.replace(":", ".");
Log.d("DEBUG::FILE NAME", filename);
try{
long lenghtOfFile = response.length;
InputStream input = new ByteArrayInputStream(response);
File path = Environment.getExternalStorageDirectory();
File file = new File(path, filename);
map.put("resume_path", file.toString());
BufferedOutputStream output = new BufferedOutputStream(new FileOutputStream(file));
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
}catch(IOException e){
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
Se si è già fatto, mettendo una barra di avanzamento è facile. ottenere ProgressDialog
oggetto e inizializzare come illustrato di seguito:
progressDialog = new ProgressDialog(Activity Context here);
progressDialog.setTitle("Any Title here");
progressDialog.setMessage("Downloading in Progress...");
progressDialog.setProgressStyle(progressDialog.STYLE_HORIZONTAL);
progressDialog.setCancelable(false);
progressDialog.setMax(100);
progressDialog.setProgress(0);
progressDialog.show();
Poi basta modificare il ciclo while come illustrato di seguito:
while ((count = input.read(data)) != -1) {
total += count;
output.write(data, 0, count);
progress = (int)total*100/file_length;
progressDialog.setProgress(progress);
}
Prova questo e fammi sapere.
Tuttavia, vorrei anche informarti che Volley non è adatto per il download pesante. Piuttosto ti suggerisco di usare DownloadManager
o Apache's HttpClient
o anche AsyncTask
. Sono più facili da usare e probabilmente migliori per questo scopo.
fonte
2016-06-13 14:15:51
Grazie. La tua soluzione mi ha aiutato. – Newbie
ur welcome .. Happy Coding !!! –
non viene chiamato 'onResponse' dopo che il file è stato scaricato completamente? –