1
ダウンロード速度の計算を計算しようとしています。 Stackoverflowで多くの情報を見つけましたが、何も助けてくれません。 最終的な計算は論理ではありません。 スレッドのダウンロード速度を知ろうとしています。 見つけたコードを添付しました。Androidでダウンロード速度テストを計算する
public void run() {
OutputStream out = null;
URLConnection conn = null;
InputStream in = null;
try
{
URL url1 = new URL("test");
out = new BufferedOutputStream(new FileOutputStream(getVideoFile().getPath()));
conn = url1.openConnection();
in = conn.getInputStream();
long start = System.currentTimeMillis();
byte[] buffer = new byte[1024];
int numRead;
long numWritten = 0;
while ((numRead = in.read(buffer)) != -1)
{
out.write(buffer, 0, numRead);
numWritten += numRead;
long end = System.currentTimeMillis();
if ((end - start)>0) {
double rate = 1000f * numWritten/(end - start) ;
Log.d("downloadmanager","speed "+rate);
}
}
}
catch (Exception ex)
{
Log.d("downloadmanager","Unknown Error: " + ex);
}
finally
{
try
{
if (in != null)
{
in.close();
}
if (out != null)
{
out.close();
}
}
catch (IOException ex)
{
Log.d("downloadmanager", "Unknown Error: " + ex);
}
}
}
}).start();
ありがとうございます。
*は論理*ではありませんか? – Blackbelt
数字が非常に高く、計算が正しいかどうかわかりません。 –