gpt4 book ai didi

java - 如何在共享首选项中保存缩放后的位图?

转载 作者:行者123 更新时间:2023-12-01 08:05:27 25 4
gpt4 key购买 nike

我试图将位图保存在共享首选项中,但是当我尝试加载该位图时,我给出了“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:

我的整个 Activity 代码:

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放在正确的位置。

最佳答案

我不认为共同偏好导致了这个问题。您只在sharedPreference 中存储位图的路径。内存不足错误很可能是由

引起的
     img_logo.setImageBitmap(bmp); 

使用一些好的图像加载库,例如 picasso或通用图像加载器。

关于java - 如何在共享首选项中保存缩放后的位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22059779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com