0

zipファイルをダウンロードしてsdcardに保存しようとしています。

ID「ダウンロード」のボタンがあります

ボタンをクリックすると、ダイアログが表示され、すぐに消えます。

コード セクションは次のようになります。

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

public void download(View view) {
    switch (view.getId()) {
        case R.id.download:
            startDownload();
    }
}

private void startDownload() {
    String url = "https://mydownloadurl";
    new DownloadFileAsync().execute(url);
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
        case DIALOG_DOWNLOAD_PROGRESS:
            mProgressDialog = new ProgressDialog(this);
            mProgressDialog.setMessage("Downloading file..");
            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            mProgressDialog.setCancelable(false);
            mProgressDialog.show();
            return mProgressDialog;

        default:
            return null;
    }
}

class DownloadFileAsync extends AsyncTask<String, String, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        showDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

    @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();
            Log.d("ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile);
            InputStream input = new BufferedInputStream(url.openStream());
            OutputStream output = new FileOutputStream("/sdcard/mydownload.zip");
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read(data)) != -1) {
                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(String... progress) {
        Log.d("ANDRO_ASYNC",progress[0]);
        mProgressDialog.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {
        dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
    }

そして、logcatに表示される唯一のものは

17586-17586/com.tyler.myapp D/qdmemalloc: イオン: マップされたバッファー ベース:0x6da9b000 サイズ:1892352 オフセット:0 fd:47 17586-17586/com.tyler.myapp D/qdmemalloc: イオン: マップされたバッファー ベース:0x4002a000 サイズ:4096 オフセット:0 fd:52 17586-17586/com.tyler.myapp D/OpenGLRenderer: キャッシュのフラッシュ (モード 0) 17586-17586/com.tyler.myapp D/qdmemalloc: イオン: アンマッピング バッファー base:0x6da9b000 size:1892352 17586-17586/com.tyler.myapp D/qdmemalloc: イオン: アンマッピング バッファ ベース:0x4002a000 サイズ:4096 596-5470/system_process W/InputMethodManagerService: ウィンドウは既にフォーカスされており、次のフォーカス ゲインを無視しています: com.android.internal.view.IInputMethodClient $Stub$Proxy@42942fe8 属性 = null、トークン = android.os.BinderProxy@41dab9b0、pid = 17586、inputType = 0x(null)

4

1 に答える 1