当我将编码的Bitmap发送到另一个类时,我遇到了这个愚蠢的内存问题:
java.lang.OutOfMemoryError:无法分配5280012字节分配3645732个空闲字节和3MB直到OOM
(之前有用,但我现在不知道它有什么问题)
这是我的发件人类:
public void ProfilePic(View view) {
Intent i = new Intent(Intent.ACTION_PICK , MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i , SELECTED_IMAGE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode){
case SELECTED_IMAGE:
if (resultCode==RESULT_OK){
Uri uri=data.getData();
String[]projection = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(uri , projection , null , null , null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(projection[0]);
String filePath=cursor.getString(columnIndex);
cursor.close();
yourImage = BitmapFactory.decodeFile(filePath);
ProfilePic = new BitmapDrawable(yourImage);
imageView.setImageDrawable(ProfilePic);
}
break;
}
}
public static String encodeTobase64(Bitmap yourImage) {
Bitmap immage = yourImage;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
immage.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
String imageEncoded = Base64.encodeToString(b, Base64.DEFAULT);
Log.d("Image Log:", imageEncoded);
return imageEncoded;
}
public void next2(View view) {
if(yourImage != null) {
SharedPreferences prefs = this.getSharedPreferences(
"PP", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("PP", encodeTobase64(yourImage));
edit.commit();
}
else {
SharedPreferences prefs = this.getSharedPreferences(
"PP", Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putString("PP", "0");
edit.commit();
}
SharedPreferences p = this.getSharedPreferences(
"home_button", Context.MODE_PRIVATE);
SharedPreferences.Editor e = p.edit();
e.putBoolean("fr", true);
e.commit();
Intent i = new Intent(this , Results.class);
startActivity(i);
}
输入“结果” - 活动时应用崩溃。
但是,当我关闭应用程序并再次返回时,它会直接进入Results-activity,一切看起来都很好(已成功检测到已发现的图片)......
导致此问题的原因是什么?
答案 0 :(得分:2)
BitmapFactory.Options
将帮助您简单地减小位图的大小。您可以手动决定大小,也可以只使用inSampleSize
字段设置为所需值。点击load large bitmap efficiently
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=4;
yourImage =BitmapFactory.decodeFile(filePath,options)