Android 从库中选择图像时出错
我从gallery中选择700kb图像并收到IllegalArgumentException。我将此链接到usage ofAndroid 从库中选择图像时出错,android,illegalargumentexception,recycle,Android,Illegalargumentexception,Recycle,我从gallery中选择700kb图像并收到IllegalArgumentException。我将此链接到usage ofrecycle()以节省一些内存。当我开始摆脱记忆错误时,这可能是一个实际问题,所以我想我必须使用回收。我叫错了吗?或者有一些规则如何正确使用它 错误: 07-02 10:25:27.466: E/AndroidRuntime(2422): FATAL EXCEPTION: main 07-02 10:25:27.466: E/AndroidRuntime(2422): ja
recycle()
以节省一些内存。当我开始摆脱记忆错误时,这可能是一个实际问题,所以我想我必须使用回收。我叫错了吗?或者有一些规则如何正确使用它
错误:
07-02 10:25:27.466: E/AndroidRuntime(2422): FATAL EXCEPTION: main
07-02 10:25:27.466: E/AndroidRuntime(2422): java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
07-02 10:25:27.466: E/AndroidRuntime(2422): at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:778)
07-02 10:25:27.466: E/AndroidRuntime(2422): at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:117)
if (requestCode == 31 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
pho1.setImageBitmap(bitmap);
// ImageView imageView = (ImageView) findViewById(R.id.imgView);
// imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
//
// Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] byte_arr = stream.toByteArray();
photo1 = Base64.encodeToString(byte_arr, Base64.DEFAULT);
bitmap.recycle();
bitmap = null;
byte_arr=null;
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
电话:
onResult:
07-02 10:25:27.466: E/AndroidRuntime(2422): FATAL EXCEPTION: main
07-02 10:25:27.466: E/AndroidRuntime(2422): java.lang.IllegalArgumentException: Cannot draw recycled bitmaps
07-02 10:25:27.466: E/AndroidRuntime(2422): at android.view.GLES20Canvas.drawBitmap(GLES20Canvas.java:778)
07-02 10:25:27.466: E/AndroidRuntime(2422): at android.view.GLES20RecordingCanvas.drawBitmap(GLES20RecordingCanvas.java:117)
if (requestCode == 31 && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
pho1.setImageBitmap(bitmap);
// ImageView imageView = (ImageView) findViewById(R.id.imgView);
// imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
//
// Bitmap bitmap = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] byte_arr = stream.toByteArray();
photo1 = Base64.encodeToString(byte_arr, Base64.DEFAULT);
bitmap.recycle();
bitmap = null;
byte_arr=null;
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
请删除位图。回收();语句。抱歉,但有什么区别吗?您正在尝试将图像转换为字符串。。。看看这个ans:我明白了,如果我想显示图像,我不能回收。但在我之前,在将其设置为ImageView之后调用
recycle
是可以的。你为什么提到字符串转换?它是用于其他目的,工作正常,还是会干扰其他东西?