当我将编码的位图发送到另一个类时,我遇到了这个愚蠢的内存问题:
java.lang.OutOfMemoryError: Failed to allocate a 5280012 byte allocation with 3645732 free bytes and 3MB until 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);
}
应用在进入“结果” Activity 时崩溃。
但是,当我关闭应用程序并再次返回时,它直接进入结果 Activity 并且一切看起来都很好(发送的图片已成功发送)...
是什么导致了这个问题?
最佳答案
BitmapFactory.Options
将帮助您简单地减小位图的大小。您可以手动决定大小,也可以简单地使用 inSampleSize
字段设置为所需的值。点击链接到 load large bitmap efficiently
BitmapFactory.Options options=new BitmapFactory.Options();
options.inSampleSize=4;
yourImage =BitmapFactory.decodeFile(filePath,options)
关于java - SharedPreferences 不会发送位图 : Failed to allocate memory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39433702/