解码流返回0值?

5
我想要下载一张图片并显示在我的imageView组件中。为了下载,我必须使用Asyntask,并显示进度条以通知用户。问题是,在通过循环获取计算出的进度值后,我从inputStream得到了0。
   Log.d("is", "" + inputStream.available()); // ---> will have a value

            byte[] buffer = new byte[contentLenght];
            while ((read = inputStream.read(buffer)) != -1) {

                counter += read;
                publishProgress(counter);

                outputStream.write(buffer,0,read);
            }
            Log.d("is", "" + inputStream.available()); // -----> will return 0
            bmp = BitmapFactory.decodeStream(inputStream); // bmp will be empty

有没有办法获取进度条的计算值,而不是在输入流结束时得到0值?
我在这里使用Asyntask。
澄清:
当我执行以下操作时,bmp将具有一个值: imageView.setImageBitmap(bmp); 仅当我删除循环并只调用bmp = BitmapFactory.decodeStream(inputStream);时,它才会起作用。
然而,如果我在执行此操作之前放置循环,则:
bmp = BitmapFactory.decodeStream(inputStream);

图片视图将不会显示任何内容

以下是包含网络连接的完整异步任务代码

   int progressCounter;
        int contentLenght;
        int counter;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setVisibility(View.VISIBLE);

        }

        @Override
        protected Boolean doInBackground(String... params) {

            return ConnectToInternet(params[0]);
        }

        @Override
        protected void onPostExecute(Boolean aVoid) {
            //Log.d("buff",bmp.toString());
            progressBar.setVisibility(View.GONE);
            imageView.setImageBitmap(bmp);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            progressCounter =(int) (((double) values[0] / contentLenght) * 100);
            progressBar.setProgress(progressCounter);
        }

        boolean ConnectToInternet(String url){
            boolean sucessfull = false;
            URL downloadURL = null;
            HttpURLConnection connection = null;
            InputStream inputStream = null;


            try {

                downloadURL = new URL(url);
                connection = (HttpURLConnection) downloadURL.openConnection();
                inputStream = connection.getInputStream();
                contentLenght = connection.getContentLength();
                Log.d("is", "" + inputStream.available());

                int read = -1;
                byte[] buffer = new byte[contentLenght];
                while ((read = inputStream.read(buffer)) != -1) {

                    counter += read;
                    publishProgress(counter);


                }
                Log.d("is", "" + inputStream.available());
                bmp = BitmapFactory.decodeStream(inputStream);

                sucessfull = true;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                connection.disconnect();
                try {

                    inputStream.close();

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

谢谢


你下载的URL是否指向正确的页面?在你自己的端上进行一些基本验证以确认你的假设是否正确。 - JoxTraex
@JoxTraex 它指向正确的页面。如果我删除循环,它将起作用。问题是没有办法告诉用户应用正在下载图像。 - CodeAndWave
如果第一个输入流有值..那么为什么不存储该值,或者读取输入流中的字节并保持计数器呢? - JoxTraex
@JoxTraex 我也试过了,结果还是一样。即使我先读取输入流,最后的日志仍然是0。这意味着在读取输入流后,我的进度条始终为0。 - CodeAndWave
发布完整的异步任务代码。 - JoxTraex
@JoxTraex 我已经更新了问题。上传了我的异步代码。 - CodeAndWave
1个回答

2
while语句完全消耗了inputStream,因此在BitmapFactory.decodeStream(inputStream)中将没有剩余内容用于解码。

尝试这个:

boolean ConnectToInternet(String url){

    // ...

    int read;

    // contentLength may be too big,
    // so read stream in smaller chunks.
    //
    // there's a typo in contentLenght :)
    byte[] buffer = new byte[4096];

    // Object for storing partially downloaded image.
    ByteArrayOutputStream imageBaos = new ByteArrayOutputStream();

    // Initialize counter.
    counter = 0;

    while ((read = inputStream.read(buffer)) != -1) {

        counter += read;
        publishProgress(counter);

        // Store downloaded chunk.
        imageBaos.write(buffer, 0, read);
    }

    // Obtain bitmap from downloaded chunks.
    bmp = BitmapFactory.decodeByteArray(imageBaos.toByteArray(), 0, imageBaos.size());

    // ...

}

工作正常!谢谢 :D - CodeAndWave

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接