Rohit Sharma
Rohit Sharma

Reputation: 78

How can I get the URI with a custom camera in Android?

I'm switching from using the default camera to finally growing a pair and deciding to make a custom camera. It's only showing me how much I don't fully understand.

Here is basically way I have been doing things in the main activity as far as photos go, but it will no longer work:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    uiHelper.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {          
        if (requestCode == PICK_PHOTO_REQUEST) {
            if (data == null) {
                Toast.makeText(this, "General Error!", Toast.LENGTH_LONG).show();
            }
            else {

                mMediaUri = data.getData();
            }

            Log.i(TAG, "Media URI: " + mMediaUri);

        }
        else {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.setData(mMediaUri);
            sendBroadcast(mediaScanIntent);
        }

(...)

Here is the picture saving function along with a couple others of the new camera:

    (...)

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
    Log.d("Picture taken");
    String path = savePictureToFileSystem(data);
    setResult(path);
    finish();
    }

private static String savePictureToFileSystem(byte[] data) {
    File file = getOutputMediaFile();
    saveToFile(data, file);
    return file.getAbsolutePath();
}

private void setResult(String path) {
    Intent intent = new Intent();
    intent.putExtra(EXTRA_IMAGE_PATH, path);
    setResult(RESULT_OK, intent);
}

   *Credit to Paul Blundell

    (...)

What do I need to do so that the main activity can receive the image's URI in the onactivityresult instead of the path String? Are URIs even applicable when it comes to custom cameras?

Please and thanks.

Upvotes: 1

Views: 665

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57173

You use custom camera, but want to do it via Intent? OK, you have the absolute file path in

String abspath = data.getExtras().getString(EXTRA_IMAGE_PATH);
mMediaUri = Uri.fromFile(new File(abspath));

Upvotes: 1

Related Questions