sharedpreferences에 비트 맵을 저장하려고하는데 비트 맵을로드하려고 할 때 "java.lang.OutOfMemoryError"가 발생합니다.
확장되지 않은 버전을 저장하고있는 것 같습니다.
이것은 내 확장 코드입니다.
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 2048;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bmp = BitmapFactory.decodeFile(filePath, o2);
}
이것이 내가 비트 맵을 저장하는 방법입니다.
decodeFile(filePath);
img_logo.setImageBitmap(bmp);
settings = getSharedPreferences("pref", 0);
SharedPreferences.Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", filePath);
prefsEditor.commit();
뭐가 잘못 되었 니?
편집하다:
이것은 내 전체 onActivityResult 코드입니다.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
decodeFile(filePath);
img_logo.setImageBitmap(bmp);
settings = getSharedPreferences("pref", 0);
SharedPreferences.Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", filePath);
prefsEditor.commit();
}
} else {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
else if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Log.d("photos*******"," in camera take int "+capturedImageFilePath);
decodeFile(capturedImageFilePath);
if(data != null)
{
img_logo.setImageBitmap(bmp);
settings = getSharedPreferences("pref", 0);
SharedPreferences.Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", capturedImageFilePath);
prefsEditor.commit();
}
}
}
}
편집 2 :
내 전체 활동 코드 :
public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
Uri mCapturedImageURI2;
SharedPreferences settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settings = getSharedPreferences("pref", 0);
img_logo = (ImageView) findViewById(R.id.imageView1);
Log.d("SHAREDimagezzzz", "**********SHAREDzzzzzzz suckes******");
img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));
Log.d("SHAREDimage", "**********SHARED suckes******");
img_logo.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
}
});
}
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
galleryintent.setType("image/*");
galleryintent.putExtra("return-data", true);
startActivityForResult(galleryintent, GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "fileName");
mCapturedImageURI2 = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI2);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
myAlertDialog.show();
}
Bitmap bmp;
public String decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 2048;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bmp = BitmapFactory.decodeFile(filePath, o2);
return filePath;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
String bb = decodeFile(filePath);
img_logo.setImageBitmap(bmp);
Log.d("galleryimage", "**********gallery suckes******");
settings = getSharedPreferences("pref", 0);
SharedPreferences.Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", bb);
prefsEditor.commit();
}
} else {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
else if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Log.d("photos*******"," in camera take int "+capturedImageFilePath);
String bbd = decodeFile(capturedImageFilePath);
if(data != null)
{
img_logo.setImageBitmap(bmp);
Log.d("cameraimage", "**********camera suckes******");
settings = getSharedPreferences("pref", 0);
SharedPreferences.Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", bbd);
prefsEditor.commit();
}
}
}
}
}
sharedPreferences 및 prefsEditor를 올바르게 배치하지 않은 것 같습니다.
나는 어떻게 든 내 문제를 해결했습니다.
나는 이것이 내 질문에 대한 최선의 대답이 아니라는 것을 알고 있지만 작동하고 있으며 그 정도면 충분합니다! :)
sharedPrefrences를로드하기 전에 다시 한 번 확장 진행을해야합니다.
public class MainActivity extends Activity {
ImageView img_logo;
protected static final int CAMERA_REQUEST = 0;
protected static final int GALLERY_PICTURE = 1;
Uri mCapturedImageURI2;
SharedPreferences settings;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settings = getSharedPreferences("pref", 0);
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.bto2);
b2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(MainActivity.this, A2.class));
}
});
img_logo = (ImageView) findViewById(R.id.imageView1);
Log.d("SHAREDimagezzzz", "**********SHAREDzzzzzzz suckes******");
BitmapFactory.Options o3 = new BitmapFactory.Options();
o3.inJustDecodeBounds = true;
BitmapFactory.decodeFile(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher), o3);
final int REQUIRED_SIZE = 2048;
int width_tmp = o3.outWidth, height_tmp = o3.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o4 = new BitmapFactory.Options();
o4.inSampleSize = scale;
Bitmap bit1 = BitmapFactory.decodeFile(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher), o4);
img_logo.setImageBitmap(bit1);
// img_logo.setImageURI(Uri.parse(settings.getString("photo1", "android.resource://com.tiktak.babyalbum/" + R.drawable.ic_launcher)));
Log.d("SHAREDimage", "**********SHARED suckes******");
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startDialog();
}
});
}
protected void startDialog() {
AlertDialog.Builder myAlertDialog = new AlertDialog.Builder(this);
myAlertDialog.setTitle("Upload Pictures Option");
myAlertDialog.setMessage("How do you want to set your picture?");
myAlertDialog.setPositiveButton("Gallery",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
galleryintent.setType("image/*");
galleryintent.putExtra("return-data", true);
startActivityForResult(galleryintent, GALLERY_PICTURE);
}
});
myAlertDialog.setNegativeButton("Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface arg0, int arg1) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "fileName");
mCapturedImageURI2 = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI2);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
myAlertDialog.show();
}
Bitmap bmp;
public void decodeFile(String filePath) {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o);
// The new size we want to scale to
final int REQUIRED_SIZE = 2048;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
bmp = BitmapFactory.decodeFile(filePath, o2);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_PICTURE) {
if (resultCode == RESULT_OK) {
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
decodeFile(filePath);
img_logo.setImageBitmap(bmp);
Log.d("galleryimage", "**********gallery suckes******");
settings = getSharedPreferences("pref", 0);
Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", filePath);
prefsEditor.commit();
}
} else {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(getApplicationContext(), "Cancelled",
Toast.LENGTH_SHORT).show();
}
else if (requestCode == CAMERA_REQUEST) {
if (resultCode == RESULT_OK) {
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(mCapturedImageURI2, projection, null, null, null);
int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
String capturedImageFilePath = cursor.getString(column_index_data);
Log.d("photos*******"," in camera take int "+capturedImageFilePath);
decodeFile(capturedImageFilePath);
if(data != null)
{
img_logo.setImageBitmap(bmp);
settings = getSharedPreferences("pref", 0);
Editor prefsEditor = settings.edit();
prefsEditor.putString("photo1", capturedImageFilePath);
prefsEditor.commit();
}
}
}
}
}
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다