如何在Android中的sharedPreferences中存储和检索位图?

我是 Android的新手.我想将我的位图存储在sharedPreferences中.有谁能告诉我怎么可能吗?其实我的要求是,我从画廊获取图像,以及从相机拍摄照片,我在ImageView中设置了Bitmap.这些都是正常的.但是当我点击后退按钮,所有的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).在按钮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); 
                        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);
                    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中设置位图.例如Bitmap bit1 = BitmapFactory.decodeFile(strimg1);然后设置,imageView.setimageBitmap

相关文章

Android性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...