while语句导致代码无法执行

时间:2013-12-16 12:27:02

标签: android android-asynctask progressdialog

我设置的my语句导致我的代码无法执行。

我在这里使用此代码时:

while ((count = is.read(data)) != -1) {
            total += count;

            publishProgress("" + (int) ((total * 100) / lenghtOfFile));

        }

更新文件下载的进度对话栏,我需要的其他代码没有被执行。

我不确定为什么这样做会如此,如果有人可以解释和帮助,那就太好了。

完整代码:

public class SetWallpaperAsync extends AsyncTask<String, String, String> {
private Context context;
private ProgressDialog pDialog;
String image_url;
URL mImageUrl;
String myFileUrl1;
Bitmap bmImg = null;

public SetWallpaperAsync(Context context) {
    this.context = context;
}

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub

    super.onPreExecute();

    pDialog = new ProgressDialog(context);
    pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    pDialog.setMessage("Downloading Image...");
    pDialog.setIndeterminate(false);
    pDialog.setCancelable(false);
    pDialog.show();
pDialog.setMax(100);
}

@Override
protected String doInBackground(String... args) { 
    // TODO Auto-generated method stub

    InputStream is = null;

    int count;

    try {

        mImageUrl = new URL(args[0]);
        // myFileUrl1 = args[0];

        HttpURLConnection conn = (HttpURLConnection) mImageUrl
                .openConnection();
        conn.setDoInput(true);
        conn.connect();
        is = conn.getInputStream();
        int lenghtOfFile = conn.getContentLength();
        byte data[] = new byte[1024];
        long total = 0;



        while ((count = is.read(data)) != -1) {
            total += count;

            publishProgress("" + (int) ((total * 100) / lenghtOfFile));

        }


        //This code doesn't get executed when using the code above
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Config.RGB_565;
        bmImg = BitmapFactory.decodeStream(is, null, options);

    } catch (IOException e) {
        e.printStackTrace();
    }

    finally {

        if (is != null) {
            try {
                is.close();
            } catch (Exception e) {
            }
        }
    }

    return null;
}

protected void onProgressUpdate(String... progress) {

    pDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String args) {
    // TODO Auto-generated method stub

    if (bmImg == null) {

        Toast.makeText(context, "Image still loading...",
                Toast.LENGTH_SHORT).show();

        pDialog.dismiss();

    }

    else {

        WallpaperManager wpm = WallpaperManager.getInstance(context);
        try {
            wpm.setBitmap(bmImg);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        pDialog.dismiss();

        Toast.makeText(context, "Wallpaper Successfully Set!",
                Toast.LENGTH_SHORT).show();

    }
}
}

3 个答案:

答案 0 :(得分:1)

可能循环中有一些例外:

while ((count = is.read(data)) != -1) {
            total += count;

            publishProgress("" + (int) ((total * 100) / lenghtOfFile));

        }

导致其余代码未被执行。

检查catch statement是否存在异常或调试代码流以确定实际问题。

答案 1 :(得分:1)

您的while循环会消耗所有输入,即直到-1流结束。

当您尝试在此处解码来自同一流的位图时:

bmImg = BitmapFactory.decodeStream(is, null, options);

......无需解码。

答案 2 :(得分:0)

您需要将最大进度设置为内容长度...

protected String doInBackground(String... args) {
    ..........
    final int contentLength = conn.getContentLength();
    runOnUiThread(new Runnable() {

        @Override
        public void run() {
            pDialog.setMax(contentLength);
        }
    });
    .........
}