How to store an image path in the shared preferences in android?

假如想象 提交于 2019-12-23 06:40:33

问题


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?

回答1:


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);

}



回答2:


Store the path as a string.

Editor e = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
e.putString("your_preference", your_path.toString());
e.commit();

This has also been asked many time before, hence the -1




回答3:


final SharedPreferences sPreference = getSharedPreferences(
                "pref_key", MODE_PRIVATE);
        final Editor spEditor = sPreference.edit();
        spEditor.putString("img_path", mFileName);
        spEditor.commit();

The above code is useful to save your image path into shared pref. Now to retrieve the image path, Use following:

final SharedPreferences sharedPreference = getSharedPreferences(
                "pref_key", MODE_PRIVATE);
        if (sharedPreference.contains("img_path")) {
            mFileName = sharedPreference.getString(img_path,
                    null);
        }


来源:https://stackoverflow.com/questions/10814551/how-to-store-an-image-path-in-the-shared-preferences-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!