位图太大.尝试全部 [英] Bitmap too large. Tried All

查看:52
本文介绍了位图太大.尝试全部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我已经连续两天面临这个问题.我想从图库中选择一个图像并将其转换为Base64方法.我尝试过毕加索,但我的imageview很大.你能帮我么.我尝试了所有方法,但将其转换为位图然后转换为base64时,内存不足对我来说太多了.

Guys I am facing this issue since 2 days straight. I want to pick an image from gallery and convert it to Base64 method. I have tried Picasso but my imageview is large. Can you please help me. I tried everything but out of memory is too much for me when converting it to bitmap and then to base64.

BitmapDrawable drawable = (BitmapDrawable) ProfilePic.getDrawable();
                            yourSelectedIBitmapDrawable drawable = (BitmapDrawable) ProfilePic.getDrawable();
                            yourSelectedImage = drawable.getBitmap();mage = drawable.getBitmap();

将此位图转换为Base64的代码.是否有可能跳过位图并直接转换为Base64

Code which is converting this bitmap to Base64. Is there any possibility of skipping the bitmap and directly converting to Base64

private String encodeToBase64(Bitmap image) {

        Bitmap immagex = image;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        immagex.compress(Bitmap.CompressFormat.JPEG, 100, baos);
        byte[] b = baos.toByteArray();
        String imageEncoded = Base64.encodeToString(b,Base64.DEFAULT);
        //Log.e("LOOK", imageEncoded);
        return imageEncoded;
    }

个人资料Pic是Imageview的名称.

Profile Pic is name of Imageview.

编辑:所以Guyz的方法之一是使用大堆,但是Google表示除非绝对必要,否则不要使用它.为我工作的另一个答案是已接受的答案.现在我想出了自己的解决方案.从理论上讲,我正在使用Picasso将图像加载到图像视图中.那如果我可以从ImageView中提取图像呢?不是来自URI或路径,而是来自Imageview.这样,您的图像就已经被毕加索缩小了.毕加索提供回调来指示成功或失败.因此,在成功时,您可以访问ImageView的缓存并从其中提取位图.

EDIT: So guyz one of the ways is to use the large heap, but Google says you should not use that unless absolutely necessary. The other one that worked for me is the accepted answer. Now I came up with my own solution. In theory, I am using Picasso to load the image into image view. So what if I can extract the image from the ImageView. Not from URI or path, but from Imageview. This way you have an image which is already reduced by Picasso. Picasso provides Callback to for Success or failure. So on Success, you can access the cache memory of ImageView and extract the Bit map out of it.

我将很快将代码发布为答案.我尝试了一下,即使最初的35Mb图像也可以转换为位图,而不会出现内存不足的情况.

I will post the code as an answer shortly. I tried it and even an image of 35Mb originally can be converted to bitmap without Out of memory exception.

这就是我的回答: https://stackoverflow.com/a/52125006/6022584

推荐答案

您可以尝试使用缓冲区读取位图数据.像这样的东西:

You can try to read the bitmap data with a buffer. Something like that :

private String encodeToBase64(Bitmap image) {
  // get an InputStream
  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
  image.compress(CompressFormat.PNG, 0, baos); 
  byte[] bitmapdata = baos.toByteArray();
  ByteArrayInputStream bais = new ByteArrayInputStream(bitmapdata);

  // prepare a buffer to read the inputStream
  byte[] buffer = new byte[10 * ONE_KIO];  // ONE_KIO = 1024
  int bytesRead;

  // prepare the stream to encode in Base64
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  Base64OutputStream base64OutputStream = new Base64OutputStream(outputStream, Base64.DEFAULT);

  // read the inputStream
  while ((bytesRead = bais.read(buffer)) != -1) {
    base64OutputStream.write(buffer, 0, bytesRead);
  }
  base64OutputStream.close();

  // get the encoded result string
  return outputStream.toString();
}

这篇关于位图太大.尝试全部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆