Save in the image gallery taken by the camera and get way of that image. - Android

Asked

Viewed 2,851 times

0

I have an android app that uses the camera to take pictures and show on a ImageView, Only the photo is not saved in the gallery. I would like to know how to save the image in the gallery and take the path of this image that was saved, because I need it to use in another function. Below follows the code of what I have 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. As it is in the code I posted, what is being shown in ImageView is only a temporary thumbnail generated and not the actual photo.

  • @ramaral thank you for answering, but this solution does not solve my problem. What I want is for the photo taken by the camera to go straight to the gallery and after saved in the gallery I have a way to get her address. As it was done in this solution that you mentioned, it is necessary to create a folder on the smartphone’s SD card where the application will run, which cannot happen in the application that I am developing. Nevertheless, thank you for your attention and for responding!

  • The photos are not recorded in the Gallery. The Gallery is a database where it is stored, among other things, the path for photos on the device. Your app should save it to a particular location and then inform Gallery. See this reply to find out how.

  • I understood @ramaral, in fact I ended up being mistaken in this subject because I’m still layman in it. I was able to understand that the gallery is just a database that stores the paths where the images are. What I wanted was for my application to save the photos taken in one of the default folders on the device, just like the camera’s native app does. The user "Monitorlpi" was able to help me on this issue, but I thank you very much for clarifying this subject.

1 answer

1


I think this way will work, any doubt, 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();
            }
        }
    }
}
  • It worked friend, thank you very much for responding!

Browser other questions tagged

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