位图太大了。尝试了全部

问题描述 投票:0回答:2

伙计们我连续两天都面对这个问题。我想从图库中选择一个图像并将其转换为Base64方法。我尝试了毕加索,但我的imageview很大。你能帮我么。我尝试了所有东西,但是在将内容转换为位图然后再转换为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的方法之一是使用大堆,但谷歌说除非绝对必要,否则你不应该使用它。另一个对我有用的是接受的答案。现在我提出了自己的解决方案。理论上,我使用Picasso将图像加载到图像视图中。那么如果我可以从ImageView中提取图像呢?不是来自URI或路径,而是来自Imageview。这样你就拥有了毕加索已经减少的形象。毕加索为成功或失败提供回调。因此,在Success上,您可以访问ImageView的缓存并从中提取Bit映射。

我将很快发布代码作为答案。我尝试了它,甚至35Mb的图像最初可以转换为位图而没有内存不足异常。

所以这是我的ans:https://stackoverflow.com/a/52125006/6022584

android bitmap base64 out-of-memory
2个回答
0
投票

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

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

0
投票

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

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

            }
        }
© www.soinside.com 2019 - 2024. All rights reserved.