Search code examples
androidandroid-cameraandroid-gallery

Take photo without saving picture to gallery?


I have a method in my activity that starts a image_capture event

    public static void takePhoto(boolean fullSize) {

    if(isIntentAvailable(mActivity.getApplicationContext(), MediaStore.ACTION_IMAGE_CAPTURE)){
        mActivity.startActivityForResult(new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE), CAPTURE_IMAGE);
    }
    else{
        Toast.makeText(mActivity.getApplicationContext(), "Geen camera beschikbaar", Toast.LENGTH_SHORT).show();
    }
}

public static boolean isIntentAvailable(Context context, String action) {
    final PackageManager packageManager = context.getPackageManager();
    final Intent intent = new Intent(action);
    List<ResolveInfo> list =
            packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
    return list.size() > 0;
}

When the user has taken a picture, I want to get the bytes from the photo and broadcast those bytes.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode != RESULT_CANCELED) {

        case CAPTURE_IMAGE:
                byte[] photoBytes = null;

                if (data != null) {
                    Bitmap b = (Bitmap) data.getExtras().get("data");
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    b.compress(Bitmap.CompressFormat.PNG, 100, stream);
                    photoBytes = stream.toByteArray();

                    Toast.makeText(getApplicationContext(), "Foto wordt geupload..", Toast.LENGTH_SHORT).show();
                    Log.d("ShowScreenActivity", Integer.toString(photoBytes.length));

                    // Send bytearray back to server
                    BroadcastSender.sendPicture(photoBytes);
                }
                else
                    Log.d("ShowScreenActivity", "Data in intent is empty");

            break;
        }
    }
}

Everything works fine, but the image is also stored in the gallery on the device.
How can I prevent this or what do I do wrong?


Solution

  • Your image is saved to the gallery, because that is the default behaviour. To override this, you need to specify a different target folder to save your image (in the intent calling the camera), and put a .nomedia file in the folder