1

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.

LordCommanDev
  • 922
  • 12
  • 38

4 Answers4

1

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);
  }
}
1

If you are trying to get the bitmap from the path obtained from intent use this code. Bitmap myBitmap = BitmapFactory.decodeFile(<yourFilepath>);

Or as you already obtained bitmap in your first activity, you directly send bitmap through intent as follows.

https://stackoverflow.com/a/2459624/5577385

Community
  • 1
  • 1
CCL
  • 11
  • 3
1

You can do this:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
    Bitmap photo = (Bitmap) data.getExtras().get("data");

    ByteArrayOutputStream stream = new ByteArrayOutputStream();

    photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
    byte[] byteArray = stream.toByteArray();

    Intent i = new Intent(getApplicationContext(), WarikeActivity.class);
    i.putExtra("foto", byteArray);
    startActivity(i);
}

And then:

void events(){
byte[] byteArray = getIntent().getByteArrayExtra("foto");
Bitmap foto = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
imgFoto.setImageBitmap(foto);

}

Luis Sotelo
  • 129
  • 6
0

Have you tried to follow the official Android documentation about it ? http://developer.android.com/training/camera/photobasics.html

tryp
  • 1,120
  • 20
  • 26