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);
}