微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在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

原文地址:https://www.jb51.cc/android/310269.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐