如何存储在Android上的共享preferences图像路径? [英] How to store an image path in the shared preferences in android?

查看:119
本文介绍了如何存储在Android上的共享preferences图像路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了我想要的共享preferences以存储图像路径。


  1. 如何存储共享preferences里面的路径?

  2. 如何从共享preferences检索图像路径?


解决方案

所有你需要做的就是,你的图像转换为它的Base64编码字符串,再presentation:

 位图realImage = BitmapFactory.de codeStream(流);
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
realImage.com preSS(Bitmap.Com pressFormat.JPEG,100,BAOS);
字节[] B = baos.toByteArray();字符串连接codeDIMAGE = Base64.en codeToString(B,Base64.DEFAULT);
textEn code.setText(EN codeDIMAGE);共享preferences shre = preferenceManager.getDefaultShared preferences(本);
编辑编辑= shre.edit();
edit.putString(IMAGE_DATA恩codeDIMAGE);
edit.commit();

然后,检索时,将其转换回位图:

 共享preferences shre = preferenceManager.getDefaultShared preferences(本);
字符串previouslyEn codeDIMAGE = shre.getString(IMAGE_DATA,);如果(!previouslyEn codedImage.equalsIgnoreCase()){
    字节[] B = Base64.de code(previouslyEn codeDIMAGE,Base64.DEFAULT);
    位图位图= BitmapFactory.de codeByteArray的(B,0,b.length个);
    imageConvertResult.setImageBitmap(位图);
}

不过,我要告诉你,Base64编码的支持是最近才列入API8。要定位在较低版本的API,您需要先添加它。幸运的是,的这家伙已经拥有所需的教程。

此外,我要告诉你,这是一个复杂的过程和共享prefrence只使用存储数据,如用户名和密码的数据量小这就是方式,您也可以使用这样的方法:

存储图像路径(从SD卡)插入共享preferences这样的 -

 共享preferences shre = preferenceManager.getDefaultShared preferences(本);
编辑编辑= shre.edit();
edit.putString(的ImagePath,/ SD卡/ imh.jpeg);
edit.commit();

要加载图像路径,您可以使用此

 最后一个共享preferences共享preference = getShared preferences(
                preF_KEY,MODE_PRIVATE);
        如果(共享preference.contains(的ImagePath)){
            字符串mFilePath =共享preference.getString(的ImagePath,
                    空值);
        }

让你可以使用路径后:

 文件imgFile =新的文件(mFilePath);
如果(imgFile.exists()){    位图MYBITMAP = BitmapFactory.de codeFILE(imgFile.getAbsolutePath());    ImageView的MYIMAGE =(ImageView的)findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(MYBITMAP);}

I have got an image path that I want to store in the shared preferences.

  1. How do I store the path inside the shared preferences?
  2. How can I retrieve the image path from the shared preferences?

解决方案

All you have to do is, convert your image to it's Base64 string representation:

Bitmap realImage = BitmapFactory.decodeStream(stream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);   
byte[] b = baos.toByteArray(); 

String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
textEncode.setText(encodedImage);

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("image_data",encodedImage);
edit.commit();

and then, when retrieving, convert it back into bitmap:

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
String previouslyEncodedImage = shre.getString("image_data", "");

if( !previouslyEncodedImage.equalsIgnoreCase("") ){
    byte[] b = Base64.decode(previouslyEncodedImage, Base64.DEFAULT);
    Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
    imageConvertResult.setImageBitmap(bitmap);
}

However, I have to tell you that Base64 support is only recently included in API8. To target on lower API version, you need to add it first. Luckily, this guy already have the needed tutorial.

Also i have to tell you that this is a complex procedure and shareprefrence use only to store small amount of data such as user name and password that's way you can also use such a method:

store image path (from sdcard) into Share preferences like this--

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit=shre.edit();
edit.putString("imagepath","/sdcard/imh.jpeg");
edit.commit();

To load your image path you can use this

final SharedPreferences sharedPreference = getSharedPreferences(
                "pref_key", MODE_PRIVATE);
        if (sharedPreference.contains("imagepath")) {
            String mFilePath = sharedPreference.getString(imagepath,
                    null);
        }

After getting you path you can use:

File imgFile = new  File(mFilePath);
if(imgFile.exists()){

    Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

    ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
    myImage.setImageBitmap(myBitmap);

}

这篇关于如何存储在Android上的共享preferences图像路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆