Android save image path in sharedpreference – As bitmap

Android save Image path in shared preference as Bitmap and retrieve it

In many application you want to save gallery image as your layout background. You can do this with save image uri in shared preference and store it as bitmap so you can retrieve it later and show in imageView. In this example I will show you how to save image path in activity page as well as in android fragment page. Android fragment is now popular suppose your application contain some tabbed page with fragment and you want to set image from gallery in you fragment layout background. Also added, delete or remove image from sharedPrefernce in this code tutorial. So you can change your image later if user want. This android save image path example of code well worked with android newer version Android Marshmallow , Lollipop and nougat no deprecation.

Android save image path in sharedPreference Activity class source code

Get the full source code of android save image path in NextGenEarn. Know more about sharedPreference from android official page.

package write your package name here;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Base64;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

private Button addImgK;

prvate Button delImgK;
SharedPreferences preferences;
ImageView photoK;
Button gWallpaper;
private static final int RQS_OPEN_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

//Show your saved bitmap image, when app is opened or during onCreat app.
preferences = getSharedPreferences(“myprefs”, 0);
photoK = (ImageView) findViewById(R.id.addImK);
String img_str = preferences.getString(“userphoto”, BuildConfig.FLAVOR);

if (!img_str.equals(BuildConfig.FLAVOR)) {
byte[] imageAsBytes = Base64.decode(img_str.getBytes(), 0);
photoK.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
}

//StartActivityForResult on button click save image uri and store it as bitmap image.

addImgK = (Button) findViewById(R.id.add_img_k);
addImgK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setType(“image/*”);
intent.setAction(“android.intent.action.GET_CONTENT”);
startActivityForResult(Intent.createChooser(intent, “Select Picture”), 0);
}
});

//After click this button your saved image will be remove from sharedPreference.

delImgK = (Button) findViewById(R.id.del_img_k);
delImgK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), “Current image 1 removed”, Toast.LENGTH_LONG).show();
SharedPreferences preferences = getSharedPreferences(“myprefs”, 0);
if (preferences.contains(“userphoto”)) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove(“userphoto”);
editor.commit();
}
}
});

//onActivityResult after button click save image uri

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case : data.getDataString();
if (resultCode == RESULT_OK) {
try {
Bitmap bit = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
ImageView bg = (ImageView) findViewById(R.id.addImK);
Drawable drawable = new BitmapDrawable(getResources(), bit);
bg.setBackground(drawable);
//Image to string
bg.buildDrawingCache();
Bitmap bitmap = bg.getDrawingCache();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] imageC = stream.toByteArray();
//String to image
String img_str = Base64.encodeToString(imageC, 0);
//String base = img_str;

SharedPreferences preferences = getSharedPreferences(“myprefs”,MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString(“userphoto”,img_str);
editor.commit();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}

default:  }  }  }

Visit NextGenEarn Blog for popular codes.

Add a Comment

Your email address will not be published. Required fields are marked *