package flickr.usth.edu.com;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class CameraActivity extends AppCompatActivity {

    // LogCat tag
    private static final String TAG = CameraActivity.class.getSimpleName();


    // Camera activity request codes
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
    private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;

    public static final int MEDIA_TYPE_IMAGE = 1;
    public static final int MEDIA_TYPE_VIDEO = 2;

    private Uri fileUri; // file url to store image/video

    private Button btnCapturePicture, btnRecordVideo;

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

        // Changing action bar background color
        // These two lines are not needed
        getActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor(getResources().getString(R.color.action_bar))));

        btnCapturePicture = (Button) findViewById(R.id.btnCapturePicture);
        btnRecordVideo = (Button) findViewById(R.id.btnRecordVideo);

//        Capture image onClick
        btnCapturePicture.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // capture picture
                captureImage();
            }
        });

//        Record video onClick
        btnRecordVideo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // record video
                recordVideo();
            }
        });

        // Checking camera availability
        if (!isDeviceSupportCamera()) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Your device doesn't support camera",
                    Toast.LENGTH_LONG).show();
            // will close the app if the device does't have camera
            finish();
        }
    }

//    Checking device has camera hardware or not
     
    private boolean isDeviceSupportCamera() {
        if (getApplicationContext().getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) {
            // this device has a camera
            return true;
        } else {
            // no camera on this device
            return false;
        }
    }

    // Launch camera
    private void captureImage() {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }

    /**
     * Launching camera app to record video
     */
    private void recordVideo() {
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);

        // set video quality
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
        // name

        // start the video capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
    }

    /**
     * Store the file url as it will be null after returning from camera
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on screen orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }
}

@@ -2555,21 +2555,16 @@
  or not%0A
-     
 %0A    pri
@@ -4398,11 +4398,1804 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Receiving activity result method will be called after closing the camera%0A     * */%0A    @Override%0A    protected void onActivityResult(int requestCode, int resultCode, Intent data) %7B%0A        // if the result is capturing Image%0A        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) %7B%0A            if (resultCode == RESULT_OK) %7B%0A%0A                // successfully captured the image%0A                // launching upload activity%0A//                launchUploadActivity(true);%0A%0A%0A            %7D else if (resultCode == RESULT_CANCELED) %7B%0A%0A                // user cancelled Image capture%0A                Toast.makeText(getApplicationContext(),%0A                        %22User cancelled image capture%22, Toast.LENGTH_SHORT)%0A                        .show();%0A%0A            %7D else %7B%0A                // failed to capture image%0A                Toast.makeText(getApplicationContext(),%0A                        %22Sorry! Failed to capture image%22, Toast.LENGTH_SHORT)%0A                        .show();%0A            %7D%0A%0A        %7D else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) %7B%0A            if (resultCode == RESULT_OK) %7B%0A%0A                // video successfully recorded%0A                // launching upload activity%0A//                launchUploadActivity(false);%0A%0A            %7D else if (resultCode == RESULT_CANCELED) %7B%0A%0A                // user cancelled recording%0A                Toast.makeText(getApplicationContext(),%0A                        %22User cancelled video recording%22, Toast.LENGTH_SHORT)%0A                        .show();%0A%0A            %7D else %7B%0A                // failed to record video%0A                Toast.makeText(getApplicationContext(),%0A                        %22Sorry! Failed to record video%22, Toast.LENGTH_SHORT)%0A                        .show();%0A            %7D%0A        %7D%0A    %7D
 %0A%7D%0A

add activity after using camera