【问题标题】:Decode Stream returning 0 value?解码流返回 0 值?
【发布时间】:2017-05-23 10:04:11
【问题描述】:

我正在尝试下载图像,然后将其显示到我的 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);

imageView 什么都不显示

这是我的完整 Asynctask 代码,包括网络连接

   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;
        }

谢谢

【问题讨论】:

  • 您尝试下载的网址是否可能未指向正确的页面?在你这边做一些基础验证来确认你的假设。
  • @JoxTraex 指向正确的页面。如果我删除循环它将起作用。问题是无法告诉用户该应用正在下载图像。
  • 如果第一个输入流有一个值..那么为什么不存储该值,或者读取输入流中的字节并保留一个计数器?
  • @JoxTraex 我也这样做了。还是一样。即使我先读取输入流,最后一个日志也会为 0。意思是在读取 IS 后,我的进度条将始终为 0
  • 发布您的完整异步任务代码。

标签: android android-bitmap bitmapfactory


【解决方案1】:

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
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多