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);
}
I implemented the classes and changed onActivityResult, but still, when I open Activity again the empty imageView ta =(
– Joana
Altered the
onCreate()
?– ramaral
Yes, I changed the onCreate..
– Joana
I find nothing wrong. The photo, after being taken, is shown in Imageview?
– ramaral
Yeah, she only disappears when I leave Activity and go back to her
– Joana
Check the code please.
– ramaral
Now it worked! I had forgotten a little something. Thank you very much! =))
– Joana
In the method
onActivityResult()
you’re picking up onnomeFoto
to create a File and then create a Uri to obtain the path photo this is all not necessary sincenomeFoto
is the path of the photo.– ramaral