【问题标题】:Android - Load image byte by byte in imageviewAndroid - 在 imageview 中逐字节加载图像
【发布时间】:2016-12-12 12:45:48
【问题描述】:

我的图像文件很大,接近 16MB。我想在我的 imageView 中加载此图像并在添加标记后对其进行缩放。我用 subsampling-scale-image-view 进行了尝试。我正在关注以下链接 https://github.com/davemorrissey/subsampling-scale-image-view

重要的是我正在从 url 加载图像。上面的库不支持。所以我只是下载了图像并在从该本地文件加载后保存到 SD 卡。从技术上讲,这是可行的。

问题:

现在的问题是第一次下载花费了太多时间。甚至第二次也需要将近一分钟。

我的想法:

由于这个问题,我尝试逐字节加载图像。一旦图像下载 100 字节,然后在 imageView 中显示下一个从 url 下载图像的下一部分。有可能这样做吗?

目前我正在加载类似以下代码的图像:

URL url = new URL(url_);

                URLConnection conection = url.openConnection();
                conection.connect();
                // getting file length
                int lenghtOfFile = conection.getContentLength();

                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream(url.openStream(), 8192);

                // Output stream to write file

                OutputStream output = new FileOutputStream(root+"/"+ fileName);
                byte data[] = new byte[1024];

                long total = 0;
                while ((count = input.read(data)) != -1) {
                    total += count;

                    // writing data to file
                    output.write(data, 0, count);

                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        image.setImage(ImageSource.uri(root+"/"+ fileName));
                    }
                });

谁能帮我解开这个谜题?

注意: 如果除了这个库之外还有其他可能性,请添加您的建议

【问题讨论】:

  • 它来自API吗?我的意思是图片
  • 是的,它来自 API
  • 请php开发者将图片转成timthumb或mthumb格式
  • 那不可能检查我的问题。我想缩放该图像并在其中添加标记。所以我不能用拇指。
  • 我想你是直接从服务器加载文件的,为什么加载图像需要时间,只需将图像转换为 timthmub 然后你可以传递高度、宽度和质量然后得到你想要的图像质量

标签: android image file


【解决方案1】:

从未尝试过,但您可以检查一下是否可行。

以字节数组的形式从url获取数据。

data = getImageStream(url);  //should call in async Task.. 

现在将字节数组转换为位图并在 imageView 中设置。

 Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
 image.setImageBitmap(bitmap)

不写入文件。这将有助于提高一些性能。

public byte[] getImageStream(String url){      
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
  is = url.openStream ();
  byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
  int n;

  while ( (n = is.read(byteChunk)) > 0 ) {
    baos.write(byteChunk, 0, n);
  }
}
catch (IOException e) {
  System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
  e.printStackTrace ();
  // Perform any other exception handling that's appropriate.
}
finally {
  if (is != null) { is.close(); }
}
return baos.toByteArray();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-29
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-16
    相关资源
    最近更新 更多