我想将图像下载到我的Android手机并按照下载的方式进行操作。 好下载工作,但我没有找到一种方式如何跟踪它在日志标签==>我的最终目标是确定下载的速度 喜欢:
`at T=0,1s 40byte downloaded`
at T=10s 2000byt downloaded ...
这是我的代码,我希望有人会帮助我,谢谢你
public class DownloadActivity extends Activity {
private MenuItem item;
private String url = "http://10.0.2.2:8080/TestAndroid/DownloadServlet";
//http://localhost:8080/TestAndroid/DownloadServlet/logo.png
private String name = "/logo.png";
ImageView imgView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
imgView = (ImageView) findViewById(R.id.imgView1);
String[] params = new String[] {url, name};
SendHttpRequestTask task = new SendHttpRequestTask();
task.execute(params);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
item = menu.getItem(0);
return true;
}
private class SendHttpRequestTask extends AsyncTask<String, Integer, byte[]> {
@Override
protected byte[] doInBackground(String... params) {
String url = params[0];
String name = params[1];
Log.i("log_tagaaaa",url );
Log.i("log_tagaaaaa",name );
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = null;
String urlString =url+name;
Log.v("Hub", "url"+urlString);
String __url=urlString.replace(" ", "%20");
HttpGet httpGet=new HttpGet(__url);
httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
httpGet.setHeader("Accept-Language", "fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3");
httpGet.setHeader("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
httpGet.setHeader("User-Agent", "Mozilla/5.0 (Linux; U; Android 3.0.1; en-us; GT-P7100 Build/HRI83) AppleWebKit/534.13 (KHTML, like Gecko) Version/4.0 Safari/534.13");
try {
response = client.execute(httpGet);
//tableau d'octets en mémoire
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream in ;
in = response.getEntity().getContent();
if (response.getStatusLine().getStatusCode() == 200) {
byte[] b = new byte[1024];
long total =0;
String d;
long a;
//le telechargement
long startTime = System.currentTimeMillis();
int i =0 ;
while ( in.read(b) != -1)
{
baos.write(b);
a= response.getEntity().getContentLength();
d=a+"";
Log.i("log_tag_taille",d );
}
// bytes_so_far : Number of bytes download so far.
//COLUMN_BYTES_DOWNLOADED_SO_FAR : public static final String
String bytes_so_far = DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR ;
Log.i("log_tag_download",DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR );
long endTime = System.currentTimeMillis();
// a= b.length;
Log.i("log_tag_startTime",startTime +"" );
Log.i("log_tag_endTime",endTime +"" );
Log.i("log_tag_difference",endTime-startTime +"" );
HashMap<String, Object> returnhashmap =new HashMap<String, Object>();
} else {
return null ;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// getContentLength va me donner la taille totale du fichier téléchargé
//returnhashmap.put(LENGTH_BUFFER, new Long(response.getEntity().getContentLength()));
return null ;
}
protected void onProgressUpdate(Integer...progress) {
super.onProgressUpdate(progress[0]);
Log.i("Progress Update: " , progress[0].toString());
}
@Override
protected void onPostExecute(byte[] result) {
Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
String d=result.length + "";
Log.i("log_tagaaaaa onPostExecute",d );
imgView.setImageBitmap(img);
item.setActionView(null);
}
}
}
答案 0 :(得分:1)
好了一点点后,我找到了一个解决方案,我不得不改变循环,就像Shoshi说的那样, I also recommand this tutorial :
根据我的需要,这是我从tutrial改变的while循环:`
while ((count = input.read(data)) != -1) {
total += count;
long endtTime = System.currentTimeMillis();
long passed;
passedTime =endtTime - startTotalTime;
// passed=passedTime -previosTime ;
// previosTime = passed ;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress(""+(int)((total*100)/lenghtOfFile));
Log.i("log_lenghtOfFile",lenghtOfFile+"" );
Log.i("log_total",total+"" );
Log.i("log_ourcentage",(int)((total*100)/lenghtOfFile)+"" );
Log.i("log_passed_time",passedTime +"" );
// Log.i("log_Debit",passedTime +"" );
// writing data to file
// output.write(data, 0, count);
}
long endTotalTime = System.currentTimeMillis();
Log.i("log_Total_passed_time",endTotalTime-startTotalTime +"" );`
我希望它会帮助某人:)