androidandroid-intentcameraextras

Take picture with camera and send the path to other activity


I'm working on my app in Android and I'm having a little problem. On my MainActivity I take a picture and then save the path in a String, then I send this string to CameraActivity. This is my code:

btnCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

                startActivityForResult(cameraIntent, CAMERA_REQUEST);

            }
        });

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {

        Bitmap photo = (Bitmap) data.getExtras().get("data");

        Uri tempUri = getImageUri(getApplicationContext(), photo);

        String ruta = getRealPathFromURI(tempUri);
        Intent i = new Intent(getApplicationContext(), WarikeActivity.class);
        i.putExtra("ruta",ruta);
        startActivity(i);
    }

}

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

public String getRealPathFromURI(Uri uri) {
    Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    return cursor.getString(idx);
}

Then, on CameraActivity I receive the path of the picture and Im trying this to put the picture on my ImageView

void events(){
    Bundle extras = getIntent().getExtras();
    ruta = extras.getString("ruta");
    Bitmap imageBitmap = (Bitmap) extras.get(ruta);
    imgWarike.setImageBitmap(imageBitmap);
}

But the Bitmap is null. Any idea why this happen? Thanks in advance.


Solution

  • Instead of taking picture and then get it path, first choose the path you want then save image there. take a look at below code:

    private File file;
    private URI imageUri;
    
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        String name = String.valueOf(System.currentTimeMillis() + ".jpg");
        file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        if (!file.exists()) {
            file.mkdirs();
        }
        File photo = new File(file, name);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(intent, CAMERA_REQUEST);
    
    @Override
    protected void onActivityResult(int requestCode, int resultCode,    Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        imageUri = data.getData();
        Intent i = new Intent(getApplicationContext(), WarikeActivity.class);
        i.putExtra("ruta", file);
        startActivity(i);
      }
    }