如何在Android中的sharedPreferences中存储和检索位图?
我是安卓新手。我想将我的位图存储在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.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
嘿朋友们,我在这里找到了问题的解决方案,我发布了我的代码,以便其他人可以使用这个解决方案..
1)。单击按钮 - 打开相机捕获图像
2)。单击按钮 - 打开图库以选择图像
3)。静态变量
4). onActivityResult
5).在 onDestroy() 中
您必须销毁您设置的所有位图。
6).当您从sharedPrefences获取数据时,您必须将字符串转换为Bitmap,然后您可以在ImageView中设置位图。
例如,Bitmap bit1 = BitmapFactory.decodeFile(strimg1);
然后设置,imageView.setImageBitmap
Hey friends I got the solution of my problem here I post my code so that others can use this solution..
1). on button click - open camera for captureing image
2). on button cllick - open Gallery for select image
3). Static variables
4). onActivityResult
5). in onDestroy()
You have to destroy all bitmap which you setted.
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
不要将位图存储在共享首选项中。如果您需要在应用程序的生命周期内保留它,可以将其分配给
static
字段。如果您希望在设备重新启动后仍保留它,请将其放入文件或数据库中。有关更多信息,请阅读 http://developer.android.com/resources/faq/框架.html#3
Don't store bitmaps in a sharedpreference. If you need to persist it during the lifetime of your application, you can assign it to a
static
field. If you want to persist it even across device reboots, put it in a file or on the database.For more information, read http://developer.android.com/resources/faq/framework.html#3
假设位图图像为 bitmapImg,您可以将位图图像转换为如下所示的 base64String 后存储
并从 SharedPreference 中检索存储的图像,如下所示
Let's say the bitmap image as bitmapImg, you can store the bitmap image after converting into a base64String like the below
And retrieve the Stored Image from the SharedPreference like the below
您可以像这样在
SharedPreference
中添加值:您只能在
SharedPreference
中添加Boolean、Float、Int、Long、String
值。要存储图像,您应该使用设备的外部或内部存储器。
You can add values in
SharedPreference
like this:You can add only
Boolean, Float, Int, Long, String
values inSharedPreference
.To store image you should external or internal memory of device.