Save in the gallery image taken by the camera and pick up the path of that image. - Android [duplicate]

0

I have an android app that uses the camera to take photos and show in ImageView , except that the picture taken is not saved in the gallery. I would like to know how to save the image in the gallery and get the path of this saved image as I need it to use in another function. Here is the code I've done so far:

public class Capturar extends AppCompatActivity{

    private static final int CAMERA = 1;
    private ImageView imageView;
    private String caminhoDaImagem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capturar);

        imageView = (ImageView) findViewById(R.id.ivImg);
    }

    public void usarCamera(View view) {

        Intent it = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(it, CAMERA);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if (requestCode == CAMERA){
            if(resultCode == RESULT_OK){

                Bitmap bitmap = (Bitmap)intent.getExtras().get("data");
                imageView.setImageBitmap(bitmap);
            }
        }
    }

NOTE: What I want to get is the REAL image taken by the camera. Of the way it is in the code I posted, what is being shown in ImageView is just a temporary thumbnail generated and not the actual photo.

    
asked by anonymous 15.10.2017 / 11:28

1 answer

1

I think that this way will work, any questions, just post.

public class Capturar extends AppCompatActivity{

    private static final int CAMERA = 1;
    private ImageView imageView;
    private String caminhoDaImagem;
    private Uri uri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_capturar);

        imageView = (ImageView) findViewById(R.id.ivImg);
    }

    public void usarCamera(View view) {

        File diretorio = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        File imagem = new File(diretorio.getPath() + "/" + System.currentTimeMillis() + ".jpg");
        uri  = Uri.fromFile(imagem);

        Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, uri);
        startActivityForResult(intentCamera, CAMERA);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {

        if(requestCode == CAMERA){
            if(resultCode == RESULT_OK){

               Intent novaIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
               sendBroadcast(novaIntent);

               caminhoDaImagem = uri.getPath();
            }
        }
    }
}
    
17.10.2017 / 05:57