Retrieve the last saved photo and insert into an imageView every time the activity is started

5

I'm doing a job and I created a page that simulates a profile, so when a user is logged in, he can enter that page and take a picture to be saved there. So as soon as the photo is taken the imageView receives it, after the user leaves the profile page I lose the reference of it. How can I make it just quit imageView when the user takes another photo? Here is the code:

import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class VerPerfilActivity extends Activity {
Button btFoto, btOk, btVerifica;
TextView nomeTV;
String nomeFoto;
int numFoto = 0;
boolean foto = false;
ImageView ivPreview;

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

    btFoto = (Button) findViewById(R.id.btFoto);
    btFoto.setOnClickListener(btFotoListener);
    btOk = (Button) findViewById(R.id.btOk);
    btOk.setOnClickListener(btOkListener);
    ivPreview = (ImageView) findViewById(R.id.imageView1);


}

private void verifica() {
    PackageManager packageManager = VerPerfilActivity.this.getPackageManager();

    if(packageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        foto = true;
    }else {
        foto = false;
    }
}


private OnClickListener btFotoListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
            verifica();
            if (foto == true) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                i.putExtra(MediaStore.EXTRA_OUTPUT, getCaminhoArquivo());

                if(i.resolveActivity(VerPerfilActivity.this.getPackageManager()) != null) {
                    startActivityForResult(i, 34);
            }else {
                Toast.makeText(VerPerfilActivity.this, "Não há nenhuma camera!", Toast.LENGTH_SHORT).show();    
            }   
        }
    }
};

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 34) {
        if (resultCode == RESULT_OK) {
            Uri takenPhotoUri = Uri.fromFile(new File(nomeFoto));
            Bitmap takenImage = BitmapFactory.decodeFile(takenPhotoUri.getPath());
            ivPreview.setImageBitmap(takenImage);
        }else {
            Toast.makeText(this, "A foto não foi tirada!", Toast.LENGTH_SHORT).show();  
        }
    }
}

protected Uri getCaminhoArquivo() {
    File diretorioMidia = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "aps");
    if (!diretorioMidia.exists() && !diretorioMidia.mkdirs())
        Log.d("aps", "error creating the file");

        numFoto++;
        String fileName = "foto" + numFoto + ".jpg";
        nomeFoto = diretorioMidia.getPath() + File.separator + fileName;
    return Uri.fromFile(new File(nomeFoto));
}

private OnClickListener btOkListener = new OnClickListener() {

    @Override
    public void onClick(View v) {
        VerPerfilActivity.this.finish();
    }
};  

I tried to insert this code into OnCreate, but it did not work either:

    File imgFile = new File(Environment.getExternalStorageDirectory().getPath());

    if(imgFile.exists()){

        Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

        ivPreview.setImageBitmap(myBitmap);

    }
    
asked by anonymous 21.06.2015 / 20:42

1 answer

4

One of the possible ways is to save the photo path using SharedPreferences .

Type two methods, one to save and one to read the path.

private void savePhotoPath(String photoPath){
    SharedPreferences preferences =  PreferenceManager.getDefaultSharedPreferences(this);
    preferences.edit.putString("photoPath",photoPath).apply();
}

private String getPhotoPath(){
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
    return preferences.getString("photoPath","");
}

In the onActivityResult() method, after assigning the photo to mageView , save your path:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 34) {
        if (resultCode == RESULT_OK) {
            Bitmap takenImage = BitmapFactory.decodeFile(nomeFoto);
            ivPreview.setImageBitmap(takenImage);
            savePhotoPath(nomeFoto);
        }else {
            Toast.makeText(this, "A foto não foi tirada!", Toast.LENGTH_SHORT).show();  
        }
    }
} 

In the% cos_de% method, get the path of the photo, and if it is not empty, retrieve the photo and assign it to ImageView :

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

    btFoto = (Button) findViewById(R.id.btFoto);
    btFoto.setOnClickListener(btFotoListener);
    btOk = (Button) findViewById(R.id.btOk);
    btOk.setOnClickListener(btOkListener);
    ivPreview = (ImageView) findViewById(R.id.imageView1);

    String photoPath = getPhotoPath();
    if(photoPath != ""){
        Bitmap takenImage = BitmapFactory.decodeFile(photoPath);
        ivPreview.setImageBitmap(takenImage);
    }
}
    
21.06.2015 / 23:52