3
How can I get the image path I just uploaded to then save to the database with the path? I took the image of the Gallery, right after with this image I wanted to take the path and save in the Sqlite bank. I’m kind of a layman on the subject :(
ImageButton contactImgView;
private String imagePath;
public void tela_cuidador_cadastrar_tema(){
setContentView(R.layout.tela_cuidador_cadastrar_tema);
contactImgView = (ImageButton) findViewById(R.id.imageButton1);
//Procura Imagem da galeria
contactImgView.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Contact Image"), 1);
}
});
}
@Override
public void onActivityResult(int reqCode, int resCode, Intent data) {
super.onActivityResult(reqCode, resCode, data);
if(resCode == RESULT_OK) {
if (reqCode == 1)
contactImgView.setImageURI(data.getData());
Uri imageUri = data.getData();
imagePath = getImagePath(imageUri);
Toast.makeText(MainActivity.this, imagePath, Toast.LENGTH_LONG).show();
}
}
public String getImagePath(Uri contentUri) {
String[] campos = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(contentUri, campos, null, null, null);
cursor.moveToFirst();
String path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();
return path;
}
Only the message that was to be displayed appears blank and not the path :S .
Hello Ramaral, I edited my post with your reply and left my code as follows. But still this giving problem, I do not know if I am very lay and do not understand or really gave, please, if you can help me according to my code above, I will be very grateful
– Gabriel Santana Bonatto
You are wearing
Intent.ACTION_GET_CONTENT
and to create a Chooser, note that my code only works if you are using the app Gallery android.– ramaral
That, only that is in an Imagebutton, then the user takes from the gallery the image, appears in the Imagebutton, soon after he presses the button to register and saves in the bank the path of that image
– Gabriel Santana Bonatto
I edited the method
getImagePath()
– ramaral
I used your modified getImagePath method only that it is turning me empty yet, I see that this empty using: Toast.makeText(Mainactivity.this, imagePath, Toast.LENGTH_LONG). show();
– Gabriel Santana Bonatto
I’ve been testing the code, getting an image from GALLERY, and it works well.
– ramaral