【问题标题】:Bitmap too large. Tried All位图太大。都试过了
【发布时间】:2018-08-31 13:24:59
【问题描述】:

伙计们,我连续 2 天都面临这个问题。我想从图库中挑选一张图片并将其转换为 Base64 方法。我试过毕加索,但我的图像视图很大。你能帮我么。我尝试了所有方法,但是在将其转换为位图然后再转换为 base64 时,内存不足对我来说太多了。

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

将此位图转换为 Base64 的代码。有没有可能跳过位图直接转成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;
    }

Profile Pic 是 Imageview 的名称。

编辑:因此,guyz 的一种方法是使用大堆,但 Google 表示,除非绝对必要,否则不应使用它。另一个对我有用的是公认的答案。现在我想出了自己的解决方案。理论上,我正在使用 Picasso 将图像加载到图像视图中。那么如果我可以从 ImageView 中提取图像呢?不是来自 URI 或路径,而是来自 Imageview。这样你就有了一幅已经被毕加索缩小的图像。 Picasso 为成功或失败提供 回调。所以在 Success 时,你可以访问 ImageView 的缓存并从中提取 Bit map。

我会尽快发布代码作为答案。我试过了,即使是原来的35Mb的图像也可以转换为位图而不会出现内存不足的异常。

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

【问题讨论】:

  • 你使用了大堆吗?
  • 一种方法是使用它。和其他被接受的一个。但最好的是使用毕加索。等等,我会发布我的代码。

标签: android bitmap base64 out-of-memory


【解决方案1】:

您可以尝试使用缓冲区读取位图数据。类似的东西:

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

【讨论】:

  • 兄弟是否可以逐行给出代码。我是安卓新手。我正在从活动结果中获取图像 URi
  • 所以我可以把这个放在活动结果中吗?我很抱歉,但我是全新的 plz
  • 感谢它的工作,但有一个更好的解决方案。请参阅已编辑的问题
【解决方案2】:

这是我的有效代码。新问题是我的活动名称

if (requestCode == PICK_IMAGE) {
            if(resultCode == RESULT_OK){
                //isImageFromGallery = true;
                //isImageSelected = true;
                ImagePath = data.getData();

                Picasso.get()
                        .load(ImagePath)
                        .resize(1024,1024)
                        .onlyScaleDown()
                        .centerCrop()
                        .into(picture, new Callback(){
                            @Override
                            public void onSuccess() {
                                BitmapDrawable drawable = (BitmapDrawable) picture.getDrawable();
                                yourSelectedImage = drawable.getBitmap();
                                //isImageSelected = true;

                                Log.d("FileSize",Formatter.formatFileSize(NewIssue.this,
                                        yourSelectedImage.getByteCount()));
                            }

                            @Override
                            public void onError(Exception e) {
                                Toast.makeText(NewIssue.this, "Could Not Load Image", Toast.LENGTH_SHORT).show();
                            }
                        });

            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-02-14
    • 1970-01-01
    • 2021-03-08
    • 2020-03-22
    • 2020-05-10
    • 2018-07-29
    • 1970-01-01
    相关资源
    最近更新 更多