【问题标题】:Not able to convert image from url into byte array无法将图像从 url 转换为字节数组
【发布时间】:2017-08-25 15:09:06
【问题描述】:

我是 android 新手,我需要将图像从 url 保存到 db 并从 db 中获取。我将图像从链接转换为字节数组,但出现以下错误:

无法解析 ByteArrayBuffer

这是代码:

 private byte[] getLogoImage(String url){
    try {
             URL imageUrl = new URL(url);
             URLConnection ucon = imageUrl.openConnection();

             InputStream is = ucon.getInputStream();
             BufferedInputStream bis = new BufferedInputStream(is);

             ByteArrayBuffer baf = new ByteArrayBuffer(500);
             int current = 0;
             while ((current = bis.read()) != -1) {
                     baf.append((byte) current);
             }

             return baf.toByteArray();
     } catch (Exception e) {
             Log.d("ImageManager", "Error: " + e.toString());
     }
     return null;
}

【问题讨论】:

  • 检查你的进口
  • 请发布您的错误日志,谢谢。
  • 欢迎来到 StackOverflow!请阅读用户指南,了解如何在发布问题之前提出好问题 (stackoverflow.com/help/how-to-ask) 谢谢

标签: android arrays image


【解决方案1】:

如下更新您的方法getLogoImage()

使用AsyncTask 并从doInBackground() 调用此方法。

这是工作代码。试试这个:

private byte[] getLogoImage(String url) {

    try {

        URL imageUrl = new URL(url);
        URLConnection ucon = imageUrl.openConnection();

        InputStream is = ucon.getInputStream();

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buffer = new byte[1024];
        int read = 0;

        while ((read = is.read(buffer, 0, buffer.length)) != -1) {
            baos.write(buffer, 0, read);
        }

        baos.flush();

        return  baos.toByteArray();

    } catch (Exception e) {
        Log.d("ImageManager", "Error: " + e.toString());
    }

    return null;
}

【讨论】:

    【解决方案2】:

    使用这个

    BitmapFactory.Options options = null;
                options = new BitmapFactory.Options();
                options.inSampleSize = 3;
                Bitmap bitmap = BitmapFactory.decodeFile(url,
                        options);
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
    
                bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
                byte[] byte_arr = stream.toByteArray();
    

    【讨论】:

    • 在我使用代码为prntscr.com/eqpn24时收到此错误prntscr.com/eqpmom
    • 首先将接收到的字节转换为位图的代码非常糟糕。内存问题。无需这样做,因为您可以将接收到的字节直接放入字节数组中。正如OP正在尝试做的那样。最好帮助他。
    • 您确定文件存在于您的设备中吗?
    • 我是从服务器链接获取的
    • 你需要先下载图片然后转换成byteArray。
    【解决方案3】:

    这样试试

    Bitmap bm = null;
       InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
           byte[] imageArray = getBytes(bm);
    

    然后使用下面的方法

      public static byte[] getBytes(Bitmap bitmap) {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
        return stream.toByteArray();
    }
    

    【讨论】:

    • 首先将接收到的字节转换为位图的代码非常糟糕。内存问题。无需这样做,因为您可以将接收到的字节直接放入字节数组中。或者将它们写入字节数组输出流。
    • 那么你能解释一下 greenapps 先生我如何通过解释来做到这一点
    • 并假设如果我想先压缩我的图像然后我想获得字节数组我怎么能这样做解释我也
    猜你喜欢
    • 2019-05-18
    • 2015-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-08-16
    • 2013-05-19
    • 1970-01-01
    相关资源
    最近更新 更多