Web サイトからファイルをダウンロードして表示する単純な asynctask を開発しました。
奇妙な動作があります:
ワイヤレス接続を使用する場合
ProgressDialog は、ダウンロードの開始時に正しく表示されます。ダウンロードが終了すると
、ProgressDialog は 0% から 100%
に正しく更新され、消えて PDF ファイルが開かれます。
3G/モバイル接続を利用する場合
ProgressDialog は、ダウンロードの開始時に正しく表示されます。ダウンロードが終了すると
、ProgressDialog は 0%
のままになり、消えて PDF ファイルが正しく開かれます。
何が問題なのですか?(私は Android 2.3.7 を使用しています)
ありがとうございます!
public class TamTam extends Activity {
public ProgressDialog mProgressDialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mProgressDialog = new ProgressDialog(TamTam.this);
mProgressDialog.setIndeterminate(false);
mProgressDialog.setMax(100);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
new DownloadFileAsync().execute("http://www.*******.com/file.pdf");
}
class DownloadFileAsync extends AsyncTask<String, Integer, String> {
String fileName;
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog.show();
}
@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();
fileName="output.pdf";
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(new File(
Environment.getExternalStorageDirectory()
+ "/" + fileName));
byte data[] = new byte[1024];
count = 0;
long total = 0;
while ((count = input.read(data)) > 0) {
total += count;
publishProgress((int) ((total * 100) / lenghtOfFile));
output.write(data, 0, count);
}
output.flush();
output.close();
input.close();
} catch (Exception e) {
}
return null;
}
protected void onProgressUpdate(Integer... progress) {
mProgressDialog.setProgress(progress[0]);
}
@Override
protected void onPostExecute(String unused) {
mProgressDialog.dismiss();
showPdf(fileName);
}
}
public void showPdf(String fileName)
{
....
}
}