我是 Android 新手。我想将我的位图存储在 sharedPreferences 中。有人能告诉我如何实现吗?其实我的要求是,我从相册获取图像以及从相机拍摄照片并将 Bitmap 设置为我的 ImageView。这些都可以正常工作。但是当我点击返回按钮时,所有的 ImageView 都会变为空白。
因此,我希望在整个应用程序中存储这些位图。
有人能帮帮我吗?我非常困在这里。
谢谢。
嘿,朋友们,我在这里得到了我的问题的解决方案,我发布了我的代码,以便其他人可以使用这个解决方案。
1). 当按钮被点击时 - 打开相机拍摄图像
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
2). 当按钮被点击时 - 打开相册以选择图片
Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
galleryintent.setType("image/*");
startActivityForResult(galleryintent, IMAGE_PICK);
3). 静态变量
private static final int CAMERA_REQUEST = 0;
private static final int IMAGE_PICK = 1;
4). onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case CAMERA_REQUEST:
if(resultCode == RESULT_OK)
{
String[] projection = { MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(mCapturedImageURI, 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);
Bitmap photo_camera = BitmapFactory.decodeFile(capturedImageFilePath, options);
if(data != null)
{
img_1.setImageBitmap(photo_camera);
prefsEditor.putString(Global.PHOTO_1,capturedImageFilePath);
}
}
case IMAGE_PICK:
if(resultCode == RESULT_OK)
{
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 filePath = cursor.getString(columnIndex);
cursor.close();
// Bitmap photo = BitmapFactory.decodeFile(filePath);
Bitmap photo_gallery = BitmapFactory.decodeFile(filePath,options);
img_1.setImageBitmap(photo_gallery);
prefsEditor.putString(Global.PHOTO_1, filePath);
}
}
prefsEditor.commit();
}
5). 在onDestroy()中,您需要销毁所有设置的位图。
@Override
public void onDestroy()
{
super.onDestroy();
if(photo_camera != null)
{
photo_camera.recycle();
}
if(photo_gallery != null)
{
photo_gallery.recycle();
}
}
6). 当您从sharedPreferences获取数据时,需要将字符串转换为位图,然后才能将位图设置到ImageView中。 例如,Bitmap bit1 = BitmapFactory.decodeFile(strimg1); 然后设置,imageView.setImageBitmap
static
字段。如果想要在设备重启后仍然保持其持久性,请将其放入文件或数据库中。SharedPreferences mSharedPreferences = context.getSharedPreferences("MyPreferences", Context.MODE_PRIVATE);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmapImg.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] compressImage = baos.toByteArray();
String sEncodedImage = Base64.encodeToString(compressImage, Base64.DEFAULT);
mSharedPreferences.edit().putString("keyStoredImage",sEncodedImage);
mSharedPreferences.edit().commit();
if(mSharedPreferences.contains("keyStoredImage"))
{
String encodedImage = mSharedPreferences.getString("keyStoredImage",null);
byte[] b = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap bitmapImage = BitmapFactory.decodeByteArray(b, 0, b.length);
}
SharedPreference
中添加值:SharedPreferences pref = getSharedPreferences("abc", 0);
Editor edit = pref.edit();
edit.putBoolean(arg0, arg1);
edit.putFloat(arg0, arg1);
edit.putInt(arg0, arg1);
edit.putLong(arg0, arg1);
edit.putString(arg0, arg1);
edit.commit();
在SharedPreference
中,您只能添加Boolean、Float、Int、Long、String
这些值。
要存储图像,您应该使用设备的外部或内部存储器。