0

I am trying to capture image and then share it with apps.

private static final int CAMERA_REQUEST = 1888;

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

        Intent cameraIntent = new Intent(
               android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        startActivityForResult(cameraIntent, CAMERA_REQUEST);
        //start the camera and capture an image
}

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

        Uri imageUri = data.getData();
        //convert captured Image from Intent form to URI

        Intent shareIntent = new Intent();
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/jpeg");
        startActivity(shareIntent);
        //share the image

    }
}

But when I run this code, the image doesn't get shared. Is there a bug in my code?

Is there any other way to share an image without saving it in memory?

3
  • 3
    Most probably, in onActivityResult data.getData() returns null. Can you please check it?
    – Aleksandr
    Commented May 26, 2015 at 8:06
  • 2
    It is returning null. How can I fix it?
    – Confuse
    Commented May 26, 2015 at 8:11
  • 2
    Please, try this code: stackoverflow.com/a/22218309/1796309 , if you it will not help, i will write correct code for you
    – Aleksandr
    Commented May 26, 2015 at 8:15

2 Answers 2

0
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
Uri screenshotUri = Uri.parse("android.resource://com.android.test/*");
try {
    InputStream stream = getContentResolver().openInputStream(screenshotUri);
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
sharingIntent.setType("image/jpeg");
sharingIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
startActivity(Intent.createChooser(sharingIntent, "Share image using")); 
0

I would like to thank Alexander for helping find the solution.

Here is the working code -

private static final int CAMERA_REQUEST = 1888;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_result);
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
               .putExtra(Intent.EXTRA_STREAM,
                Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(),
                        (Bitmap) data.getExtras().get("data"), "title", null)
               ))
               .setType("image/jpeg");
        startActivity(intent);
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.