2011-09-06 55 views
9

我是Android新手。我想将我的位图存储在sharedPreferences中。谁能告诉我怎么可能?其实我的要求是,我从图库中获取图像以及从相机拍摄图片,并在ImageView中设置了该位图。这些一切工作正常。但是当我点击后退按钮时,所有ImageView都将为空。如何在Android中的sharedPreferences中存储和检索位图?

所以我想在整个应用程序中存储Bitmaps。

任何人都可以帮助我吗?我非常坚持这一点。

谢谢。

+0

你可以通过它。这一定会帮助你。 [点击这里] [1] [1]:http://stackoverflow.com/questions/17268519/how-to-store-bitmap-object-in-sharedpreferences-in-android –

回答

11

嘿朋友我来到这里我的问题的解决方案我后我的代码,以便其他人可以使用此解决方案..

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)。打开按钮cllick - 打开选择图像的图库

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

+0

感谢您发布您的问题的解决方案。将您的答案设置为选中,将其从未答复的列表中删除。 – blessenm

+0

嘿@anddev你可以发布你的完整代码? –

2

您可以像这样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值。

要存储图像,您应该使用设备的外部或内部存储器。

+0

谢谢,我用按照你的说法。但现在我想将字符串转换为位图来显示我的图像。你有任何想法将字符串转换为位图? – anddev

+0

试试这个:http:// stackoverflow。com/questions/5405373 /转换字符串作为一个位图在Android –

3

不要将位图存储在sharedpreference中。如果您需要在应用程序的整个生命周期中坚持它,则可以将其分配给static字段。如果你想在设备重启时保存它,把它放在一个文件或数据库中。

欲了解更多信息,请阅读http://developer.android.com/resources/faq/framework.html#3

相关问题