位图太大。都试过了
Bitmap too large. Tried All
伙计们,我已经连续 2 天遇到这个问题了。我想从图库中挑选一张图片并将其转换为 Base64 方法。我试过 Picasso,但我的 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;
}
个人资料图片是 Imageview 的名称。
编辑:所以 guyz 的一种方法是使用大堆,但是 Google 说除非绝对必要,否则你不应该使用它。另一个对我有用的是公认的答案。现在我想出了自己的解决方案。理论上,我使用 Picasso 将图像加载到图像视图中。那么如果我可以从 ImageView 中提取图像呢?不是来自 URI 或路径,而是来自 Imageview。这样你就有了一幅已经被 Picasso 缩小过的图像。 Picasso 提供 Callback 以指示成功或失败。所以在成功时,您可以访问 ImageView 的缓存内存并从中提取位图。
我很快就会 post 代码作为答案。我试过了,即使是35Mb的图像也可以在没有内存不足异常的情况下转换为位图。
所以这是我的回答:
您可以尝试用缓冲区读取位图数据。类似的东西:
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();
}
这是我的有效代码。 New Issue 对我来说是 Activity 的名字
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();
}
});
}
}
伙计们,我已经连续 2 天遇到这个问题了。我想从图库中挑选一张图片并将其转换为 Base64 方法。我试过 Picasso,但我的 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;
}
个人资料图片是 Imageview 的名称。
编辑:所以 guyz 的一种方法是使用大堆,但是 Google 说除非绝对必要,否则你不应该使用它。另一个对我有用的是公认的答案。现在我想出了自己的解决方案。理论上,我使用 Picasso 将图像加载到图像视图中。那么如果我可以从 ImageView 中提取图像呢?不是来自 URI 或路径,而是来自 Imageview。这样你就有了一幅已经被 Picasso 缩小过的图像。 Picasso 提供 Callback 以指示成功或失败。所以在成功时,您可以访问 ImageView 的缓存内存并从中提取位图。
我很快就会 post 代码作为答案。我试过了,即使是35Mb的图像也可以在没有内存不足异常的情况下转换为位图。
所以这是我的回答:
您可以尝试用缓冲区读取位图数据。类似的东西:
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();
}
这是我的有效代码。 New Issue 对我来说是 Activity 的名字
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();
}
});
}
}