我是Android新手。我想将我的位图存储在sharedPreferences中。谁能告诉我这怎么可能?实际上,我的要求是,我从图库中获取图像,并从相机中获取图片,然后在我的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)。当您从sharedPrefrences获取数据时,必须将字符串转换为Bitmap,然后才能在ImageView中设置位图。例如,位图bit1 = BitmapFactory.decodeFile(strimg1);然后设置,imageView.setImageBitmap
不要在共享首选项中存储位图。如果您需要在应用程序的生命周期内保留它,则可以将其分配给static
字段。如果即使在设备重新启动后仍要保留它,请将其放在文件或数据库中。
有关更多信息,请阅读http://developer.android.com/resources/faq/framework.html#3
您可以像这样在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();
您只能在Boolean, Float, Int, Long, String
中添加SharedPreference
值。
要存储图像,您应该在设备的外部或内部存储器中。
假设位图图像为bitmapImg,您可以将位图图像转换为如下所示的base64String之后存储它
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();
并从SharedPreference中检索存储的图像,如下所示
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);
}