Retrieve the last saved photo and insert it into a View image every time Activity starts

Asked

Viewed 295 times

5

I’m doing a job and I created a page that simulates a profile, so when a user is logged in, they can log on to that page and take a photo 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 her reference. How can I make her just get out of imageView when the user takes another photo? Follow 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 didn’t work either:

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

    if(imgFile.exists()){

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

        ivPreview.setImageBitmap(myBitmap);

    }

1 answer

4


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

Write two methods, one to keep 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 method onActivityResult(), after assigning the photo to mageView, keep your way:

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 method onCreate() 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);
    }
}
  • I implemented the classes and changed onActivityResult, but still, when I open Activity again the empty imageView ta =(

  • Altered the onCreate()?

  • Yes, I changed the onCreate..

  • I find nothing wrong. The photo, after being taken, is shown in Imageview?

  • Yeah, she only disappears when I leave Activity and go back to her

  • Check the code please.

  • Now it worked! I had forgotten a little something. Thank you very much! =))

  • In the method onActivityResult() you’re picking up on nomeFoto to create a File and then create a Uri to obtain the path photo this is all not necessary since nomeFoto is the path of the photo.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.