代码之家  ›  专栏  ›  技术社区  ›  Nishant Garg

位图太大。都试过了

  •  0
  • Nishant Garg  · 技术社区  · 6 年前

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

    编辑 回拨

    我很快就会把代码作为答案贴出来。我试过了,甚至一个35Mb的图像本来可以转换成位图没有内存不足的例外。

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

    2 回复  |  直到 6 年前
        1
  •  0
  •   Bruno    6 年前

    您可以尝试用缓冲区读取位图数据。类似于:

    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();
    }
    
        2
  •  0
  •   Nishant Garg    6 年前

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

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