如何存储在Android上的共享preferences图像路径?路径、图像、Android、preferences

2023-09-07 13:56:24 作者:把孤独当做晚餐

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

如何存储共享preferences里面的路径?如何从共享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(位图);} 
Android开源 3分钟全面了解Android主流图片加载库

不过,我要告诉你,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.

How do I store the path inside the shared preferences? 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);

}