提问人:Marisol vega 提问时间:6/10/2023 更新时间:6/10/2023 访问量:49
为什么我得到 Canvas:试图绘制太大的位图 android 错误
Why Am I getting Canvas: trying to draw too large bitmap android error
问:
我有一个 android 应用程序,其中包含一个带有一个图像按钮、另一个按钮和一个图像视图的片段,可以使用相机设备拍照,但在 Galaxy A33 中,我收到此错误 java.lang.RuntimeException - 画布:尝试绘制太大(256576512bytes)位图。 android.graphics.RecordingCanvas.throwIfCannotDraw (RecordingCanvas.java:266)
为了显示照片,我使用从android文档中获取的此代码
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == REQUEST_CODE_TAKE_PHOTO) && resultCode == RESULT_OK) {
if (archivofoto!=null&&archivofoto.exists()) {
if(requestCode == REQUEST_CODE_TAKE_PHOTO) {
if(ComprasUtils.getAvailableMemory(getActivity()).lowMemory)
{
Toast.makeText(getActivity(), "No hay memoria suficiente para esta accion", Toast.LENGTH_SHORT).show();
return;
}else {
ComprasUtils cu = new ComprasUtils();
cu.comprimirImagen(archivofoto.getAbsolutePath());
Bitmap bitmap1 = ComprasUtils.decodeSampledBitmapFromResource(archivofoto.getAbsolutePath(), 100, 100);
imageview.setImageBitmap(bitmap1);
imageview.setVisibility(View.VISIBLE);
nombre_foto=null;
archivofoto=null;
}
}
}
else{
Log.e(TAG,"Algo salió mal???");
}
}
else
{
Log.e(TAG,"Algo salió muy mal**");
}
}
decodeSampledBitmapFromResource 函数应该压缩照片以显示它,但错误让我觉得不起作用或我做错了什么?
public static Bitmap decodeSampledBitmapFromResource( String nombre_foto,
int reqWidth, int reqHeight) {
File archivo = new File(nombre_foto);
if (!archivo.exists()) {
return null;
}
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// BitmapFactory.decodeResource(res, resId, options);
BitmapFactory.decodeFile(nombre_foto,options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeFile( nombre_foto, options);
}
答: 暂无答案
评论