Catch the path of an image in the Gallery

3

How can I do to get the path of the image I just uploaded and then save to the database with the path? I get the image from the Gallery, then with that image I wanted to take the path and save it to the SQLite database. I'm half-hearted 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.

    
asked by anonymous 15.06.2015 / 20:17

1 answer

1

If you are getting the gallery image using Intent.ACTION_PICK you can use onActivityResult() to get the Uri to image data in the Uri imageUri = intent.getData(); method. Use the getImagePath() method to read the path of it.

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;
}
    
15.06.2015 / 23:26