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

How to store and retrieve bitmap in sharedPreferences in Android?(如何在 Android 的 sharedPreferences 中存储和检索位图?)

本文介绍了如何在 Android 的 sharedPreferences 中存储和检索位图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am new in Android. I want to store my bitmap in sharedPreferences. Can anyone tell me how it will possible? Actually my requirements is, I fetch the image from gallery as well as take picture from camera and I set that Bitmap in my ImageView. These all things work properly. But when I click on back button all ImageView will be empty.

所以我想在整个应用程序中存储这些位图.

So I want to store that Bitmaps throughout my application.

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

Can anyone help me? I am very much stuck on this.

谢谢.

推荐答案

嘿朋友们,我在这里找到了我的问题的解决方案,我发布我的代码,以便其他人可以使用这个解决方案..

Hey friends I got the solution of my problem here I post my code so that others can use this solution..

1).点击按钮 - 打开相机以捕捉图像

1). on button click - open camera for captureing image

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).单击按钮 - 打开图库以选择图像

2). on button cllick - open Gallery for select image

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).活动结果

    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()您必须销毁您设置的所有位图.

5). in onDestroy() You have to destroy all bitmap which you setted.

@Override
    public void onDestroy()
    {
        super.onDestroy();
        if(photo_camera != null)
        {
            photo_camera.recycle();
        }
        if(photo_gallery != null)
        {
            photo_gallery.recycle();
        }
}

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

6). At the time when you fetch data from sharedPrefrences you have to convert string in to Bitmap and then you can set bitmap in ImageView. for example, Bitmap bit1 = BitmapFactory.decodeFile(strimg1); and then set , imageView.setImageBitmap

这篇关于如何在 Android 的 sharedPreferences 中存储和检索位图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何在 Android 的 sharedPreferences 中存储和检索位图?

基础教程推荐