【问题标题】:While statement causing code to not get executedWhile 语句导致代码无法执行
【发布时间】:2013-12-16 12:27:02
【问题描述】:

我设置的 while 语句导致它下面的代码无法执行。

当我在这里使用此代码时:

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();

    }
}
}

【问题讨论】:

  • 没听懂 - 什么是 publishProgress()?
  • @Jack 为什么需要读取流的字节,因为您没有将字节分配给任何其他字节?直接您可以获取 InputStream 并将该 Stream 传递给 BitmapFactory....

标签: android android-asynctask progressdialog


【解决方案1】:

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

当您在此处尝试从同一流中解码位图时:

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

...没有什么可以解码的。

【讨论】:

  • 克隆输入流是否是个好主意?还是有其他方法可以做到?
  • @Jack 使用进度感知输入流,即子类化流类并覆盖 read 以发布进度通知。
【解决方案2】:

循环中可能有一些异常:

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

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

        }

这导致其余代码未执行。

检查catch statement是否有一些异常或调试代码流以识别实际问题。

【讨论】:

    【解决方案3】:

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

    protected String doInBackground(String... args) {
        ..........
        final int contentLength = conn.getContentLength();
        runOnUiThread(new Runnable() {
    
            @Override
            public void run() {
                pDialog.setMax(contentLength);
            }
        });
        .........
    }
    

    【讨论】:

    • 他的问题具有误导性。我也认为他的抱怨是没有显示进度更新,但是在阅读他的代码时,我发现他遇到的问题是 follows while 循环没有运行——我假设@ 987654322@ 永远不会从他的投诉中恢复过来。
    • @mah 让我们看到 Jack 对这个答案的回复……实际上他是在 while 循环中调用 publishProgress()。当 while 循环第一次执行时,这里的进度将是 1024...
    • while 循环正在运行。使用 while 代码时,由于某些奇怪的原因,它下面的代码没有被执行
    • 确保您的 URL 正在返回响应。你的循环是正确的。 while ((count = is.read(data)) != -1) 或 while ((count = is.read(data)) >0 ) 应该可以正常工作。
    • @Jack 为什么需要读取流的字节,因为您没有将字节分配给任何其他字节?直接您可以获取 InputStream 并将该 Stream 传递给 BitmapFactory...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-11
    • 2013-05-02
    • 2021-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多