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

and*_*dev 10 android imageview

我是Android新手.我想将我的位图存储在sharedPreferences中.谁能告诉我它怎么可能呢?实际上我的要求是,我从图库中获取图像以及从相机拍摄照片,然后在我的ImageView中设置了Bitmap.这些都能正常运作.但是当我点击后退按钮时,所有ImageView都将为空.

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

谁能帮我?我非常坚持这一点.

谢谢.

and*_*dev 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);
Run Code Online (Sandbox Code Playgroud)

2).按钮cllick - 打开选择图像的图库

Intent galleryintent = new Intent(Intent.ACTION_GET_CONTENT);
galleryintent.setType("image/*");
startActivityForResult(galleryintent, IMAGE_PICK);
Run Code Online (Sandbox Code Playgroud)

3).静态变量

private static final int CAMERA_REQUEST = 0; 
    private static final int IMAGE_PICK = 1;
Run Code Online (Sandbox Code Playgroud)

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();
}
Run Code Online (Sandbox Code Playgroud)

5).在onDestroy()中你必须销毁你设置的所有位图.

@Override
    public void onDestroy()
    {
        super.onDestroy();
        if(photo_camera != null)
        {
            photo_camera.recycle();
        }
        if(photo_gallery != null)
        {
            photo_gallery.recycle();
        }
}
Run Code Online (Sandbox Code Playgroud)

6).当您从sharedPrefrences获取数据时,您必须将字符串转换为Bitmap,然后您可以在ImageView中设置位图.例如,Bitmap bit1 = BitmapFactory.decodeFile(strimg1); 然后设置imageView.setImageBitmap