Manoj S Seeker
Manoj S Seeker

Reputation: 11

getOutputMediaFileUri method not recognized in android camera intent

I am running a new Intent to access the camera in Android, but I'm getting this error:

getOutputMediaFileUri method not recognized

The code is below

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); 
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); 


startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Upvotes: 0

Views: 2962

Answers (3)

E J Chathuranga
E J Chathuranga

Reputation: 925

You haven't read correctly the article. You have to add below methods into your MainActivity.java

/**
 * Creating file uri to store image/video
 */
public Uri getOutputMediaFileUri(int type) {
    return Uri.fromFile(getOutputMediaFile(type));
}

/*
 * returning image / video
 */
private static File getOutputMediaFile(int type) {

    // External sdcard location
    File mediaStorageDir = new File(
            Environment
                    .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
            IMAGE_DIRECTORY_NAME);

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                    + IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

    // Create a media file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
            Locale.getDefault()).format(new Date());
    File mediaFile;
    if (type == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "IMG_" + timeStamp + ".jpg");
    } else if (type == MEDIA_TYPE_VIDEO) {
        mediaFile = new File(mediaStorageDir.getPath() + File.separator
                + "VID_" + timeStamp + ".mp4");
    } else {
        return null;
    }

    return mediaFile;
}

Upvotes: 0

Anil Meenugu
Anil Meenugu

Reputation: 1431

Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
       File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
       imagesFolder.mkdirs(); // <----
       File image = new File(imagesFolder, "image_001.jpg");
       Uri uriSavedImage = Uri.fromFile(image);
       imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
       startActivityForResult(imageIntent,0);

Now this will store your camera captured images in MyImages directory in sdcard with image_001.jpg name.

Upvotes: 3

Your code seems to come from this Android Camera tutorial.

It says there:

The getOutputMediaFileUri() method in this example refers to the sample code shown in Saving Media Files.

And in that link, we find the code:

/** Create a file Uri for saving an image or video */
private static Uri getOutputMediaFileUri(int type){
      return Uri.fromFile(getOutputMediaFile(type));
}

So add the above snippet to your code, and you should be good to go.

Upvotes: 0

Related Questions