Take the path of an image in the Gallery

Asked

Viewed 2,393 times

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 .

1 answer

1


If you are downloading the Gallery image using Intent.ACTION_PICK may, in the method onActivityResult(), use Uri imageUri = intent.getData(); to obtain the Uri to image data. Use the method getImagePath() to read the path hers.

private String imagePath;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    switch (requestCode) {
    case REQUEST_PICK_IMAGE:
        if(RESULT_OK == resultCode){
            Uri imageUri = intent.getData();
            imagePath = getImagePath(imageUri)
            ......
            ......
        break;

    default:
        break;
    }
}

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

  • 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.

  • 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

  • I edited the method getImagePath()

  • 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();

  • I’ve been testing the code, getting an image from GALLERY, and it works well.

Show 1 more comment

Browser other questions tagged

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