Search code examples
androidandroid-camera-intent

How to get a Result from Image Capture and save it to Shared Preferences in Android


I am building an app in which I want when the user clicks the imageview, the device camera app to launch and take a picture and set it to the imageview. I have accomplished that!!! But when I exit the app and re-open it the image has been deleted. How can I do it so the image is permanent?

My code:

private SessionManager session;
private static final int REQ_CODE = 1152;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
private Uri file;
private String fileName = "ImageTaken";
private ImageView imagePhoto;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_data);
        session = new SessionManager(getApplicationContext());
        session.checkLogin();
        HashMap<String, String> user = session.getUserDetails();
        Toolbar tb = (Toolbar)findViewById(R.id.topBar);
        setSupportActionBar(tb);
        String name = user.get(SessionManager.KEY_NAME);
        TextView watersName = (TextView)findViewById(R.id.waitersName);
        TextView date = (TextView)findViewById(R.id.date);
        TextView time = (TextView)findViewById(R.id.time);
        watersName.setText(Html.fromHtml("<b>" + name + "</b>"));
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("dd" + "/" + "mm" + "/" + "yyyy");
        String text = dateFormat.format(c.getTime());
        date.setText(text);
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        String timeF = timeFormat.format(c.getTime());
        time.setText(timeF);
        Button btnnewworder = (Button)findViewById(R.id.btnnewworder);
        btnnewworder.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserData.this, Tables.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
            }
        });
        Button payment = (Button)findViewById(R.id.btnpayment);
        payment.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DialogMessageDisplay.displayInfoMessage(UserData.this, "Payment Details", "There was no order made. \n Make an order and then tap the payment button.", AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);

            }
        });
        imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
        imagePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ContentValues values = new ContentValues();
                values.put(MediaStore.Images.Media.TITLE, fileName);
                file = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                //file = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
                intent.putExtra(MediaStore.EXTRA_OUTPUT, file);
                startActivityForResult(intent, REQ_CODE);
            }
        });
    }

OnActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == REQ_CODE){
            if(resultCode == RESULT_OK){


                imagePhoto.setImageURI(file);
                editor.putString(fileName, file.toString());
                editor.commit();
                Toast.makeText(UserData.this, "Your file has been saved at " + file, Toast.LENGTH_SHORT).show();

            }else if(resultCode == RESULT_CANCELED){
                Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
            }
        }
    }

Thanks in advance!!!


Solution

  • Try this:

    1) Initialize these:

    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    

    2) Modify your onCreate() like:

    sharedPreferences = getSharedPreferences(fileName, MODE_PRIVATE);
    editor = sharedPreferences.edit();
    imagePhoto = (ImageView)findViewById(R.id.waiterPhoto);
    String commited = sharedPreferences.getString(fileName, null);
    if(commited != null) {
        imagePhoto.setImageURI(Uri.parse(commited));
    }
    

    3) Modify your onActivityResult() like:

    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if(requestCode == REQ_CODE){
                if(resultCode == RESULT_OK){
                    imagePhoto.setImageURI(file);
                    editor.putString(fileName, file.toString());
                    editor.commit();
                }else if(resultCode == RESULT_CANCELED){
                    Toast.makeText(UserData.this, "Action Cancelled", Toast.LENGTH_SHORT).show();
                }
            }
        }
    

    And then you will have your desired effect. Hope it helps!!!