-1

I have captured image from camera and upload via upload API with retrofit.now i have got successfully response like:

{ "msg": "Image Upload Successful", "status": 1, "image": "115648975487_1551435779.png" }

I have confused to save "image" name in shared preference and send this name as parameter in next API calling.

here is my code for upload image through Retrofit API:

public void imageUpload(final String imageEncoded){
    file = new File(imageEncoded);
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    builder.addFormDataPart("image", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));

    ApiCall.callPostRetrofit(context, AppConstants.uploadImage, builder, new ApiCallBackListner() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String msg = jsonObject.getString("msg");
                String status = jsonObject.getString("status");

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(String error) {

        }
    });
}
Ravi
  • 28
  • 6

1 Answers1

0

this class consists saveImage() and getUser() methods for saving or getting value from shared preference.

public class SessionManager {
    SharedPreferences pref;
    SharedPreferences.Editor editor;
    Context _context;
    int PRIVATE_MODE = 0;
    private static final String PREF_NAME = "Pref";
    public static  final String KEY_IMAGE="image";




    public SessionManager(Context context) {
        this._context = context;
        pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
        editor = pref.edit();
    }

    public void saveImage(String imagename) {
        editor.putString(KEY_IMAGE,imagename);
        editor.commit();
    }

    public String getuser(String KeyName)
    {
        return pref.getString(KeyName,null);
    }




}

then,

public void imageUpload(final String imageEncoded){
    SessionManager session=new SessionManager(this);

    file = new File(imageEncoded);
    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    builder.addFormDataPart("image", file.getName(), RequestBody.create(MediaType.parse("multipart/form-data"), file));

    ApiCall.callPostRetrofit(context, AppConstants.uploadImage, builder, new ApiCallBackListner() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                String msg = jsonObject.getString("msg");
                String status = jsonObject.getString("status");
                String image=jsonObject.getString("image");
                session.saveImage(image);// saving image name to shared preference.

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onError(String error) {

        }
    });
}

for getting image name from sharedpreference,write only some line of code in that activity or fragment:-

SessionManager session=new SessionManager(context);
session.getuser(SessionManager.KEY_IMAGE );