Android Open Source - CameraApp-Android Main Activity






From Project

Back to project page CameraApp-Android.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project CameraApp-Android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.devcfgc.cameraapp;
/*from   www .  j  a  v  a2  s  . c  o  m*/
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

public class MainActivity extends Activity {

  private static final String TAG = MainActivity.class.getSimpleName();

  public static final int TAKE_PHOTO_REQUEST = 0;

  public static final int MEDIA_TYPE_IMAGE = 4;

  protected Uri mMediaUri;

  // Listener for camera icon button
  protected DialogInterface.OnClickListener mDialogListener = new DialogInterface.OnClickListener() {

    @Override
    public void onClick(DialogInterface dialog, int which) {
      switch (which) {
      case 0:// Take picture
        Intent takePhotoIntent = new Intent(
            MediaStore.ACTION_IMAGE_CAPTURE);
        mMediaUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
        if (mMediaUri == null) {
          // display an error if can't access to external storage
          Toast.makeText(MainActivity.this,
              R.string.error_external_storage, Toast.LENGTH_LONG)
              .show();
        } else {
          takePhotoIntent
              .putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
          startActivityForResult(takePhotoIntent, TAKE_PHOTO_REQUEST);
        }

        break;
      }
    }
  };

  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK) {
      // Create a Broadcast to inform that the image is available
      Intent mediaScanIntent = new Intent(
          Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
      mediaScanIntent.setData(mMediaUri);
    }

  }

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

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int itemId = item.getItemId();

    switch (itemId) {
    case R.id.action_settings:
      return true;

    case R.id.action_camera:
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      builder.setItems(R.array.camera_choices, mDialogListener);
      AlertDialog dialog = builder.create();
      dialog.show();
    }

    return super.onOptionsItemSelected(item);
  }

  private Uri getOutputMediaFileUri(int mediaType) {
    // To be safe, you should check that the SDCard or ExternalStore is mounted
    // using Environment.getExternalStorageState() before doing this.
    if (isExternalStorageAvailable()) {
      // get the URI

      // 1. Get the external storage directory
      String appName = MainActivity.this.getString(R.string.app_name);
      File mediaStorageDir = new File(
          Environment
              .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
          appName);

      // 2. Create our subdirectory
      if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
          Log.e(TAG, "Failed to create directory.");
          return null;
        }
      }

      // 3. Create a file name with the string and a date
      File mediaFile;
      Date now = new Date();
      String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
          Locale.US).format(now);

      String path = mediaStorageDir.getPath() + File.separator;
      if (mediaType == MEDIA_TYPE_IMAGE) {
        mediaFile = new File(path + "IMG_" + timestamp + ".jpg");
      } else {
        return null;
      }

      // 4. Return the file's URI
      return Uri.fromFile(mediaFile);
    } else {
      return null;
    }
  }

  private boolean isExternalStorageAvailable() {
    String state = Environment.getExternalStorageState();
    if (state.equals(Environment.MEDIA_MOUNTED)) {
      return true;
    } else {
      return false;
    }
  }

}




Java Source Code List

com.devcfgc.cameraapp.MainActivity.java