Android Open Source - My-Team--Your-Team Camera Activity






From Project

Back to project page My-Team--Your-Team.

License

The source code is released under:

MIT License

If you think the Android project My-Team--Your-Team 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.selesse.apps.activity;
/*from   w  w  w  . jav a  2s.c  om*/
import java.io.File;
import java.io.IOException;
import java.util.List;

import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.ShutterCallback;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.MediaScannerConnectionClient;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.Toast;

import com.selesse.apps.EquipePictureCallback;
import com.selesse.apps.Preview;
import com.selesse.apps.R;
import com.selesse.apps.model.SimpleCamera;
import com.selesse.apps.model.Team;

public class CameraActivity extends Activity implements MediaScannerConnectionClient {
  private static Preview preview;
  private Button tonEquipeBtn;
  private Button monEquipeBtn;
  private Bitmap tonEquipeBmp;
  private Bitmap monEquipeBmp;
  private String imagePath;
  public static String picFolder;
  public static List<SimpleCamera> cameraInfo;
  public static Context context;
  public static boolean systemHasCameraWithFlash = false;
  MediaScannerConnection conn = null;

  /** Called when the activity is first created. */
  @TargetApi(11)
  @Override
  public void onCreate(Bundle savedInstanceState) {
    context = getApplicationContext();
    cameraInfo = SimpleCamera.getCameraInfoList();

    // If there's an SD card, write to Camera
    if (android.os.Environment.getExternalStorageState().equals(
        android.os.Environment.MEDIA_MOUNTED)) {
      picFolder = Environment.getExternalStorageDirectory().getPath() + "/Pictures/MyTeamYourTeam/";
    } // otherwise, we're using internal storage
    else {
      picFolder = "/Pictures/MyTeamYourTeam/";
    }

    // load the "ton/mon equipe" bitmaps; show error message if it fails
    try {
      tonEquipeBmp = BitmapFactory.decodeStream(getAssets().open("tonequipe.png"));
      monEquipeBmp = BitmapFactory.decodeStream(getAssets().open("monequipe.png"));
    }
    catch (IOException e) {
      popup(R.string.asset_load_fail);
    }

    // if we support the Action Bar, bring it in
    if (Build.VERSION.SDK_INT >= 11) {
      requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
      ActionBar actionBar = getActionBar();
      if (actionBar != null) {
        actionBar.setDisplayOptions(ActionBar.DISPLAY_USE_LOGO);
      }
      else {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
      }
    }
    else {
      requestWindowFeature(Window.FEATURE_NO_TITLE);
    }
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Preview object is what previews the actual camera
    preview = new Preview(this);

    ((FrameLayout) findViewById(R.id.preview)).addView(preview);

    tonEquipeBtn = (Button) findViewById(R.id.bTonEquipeClick);
    monEquipeBtn = (Button) findViewById(R.id.bMonEquipeClick);

    // add an onClick listener to the buttons that will snap a picture
    tonEquipeBtn.setOnClickListener(createOnClickListener(tJpegCallback));
    monEquipeBtn.setOnClickListener(createOnClickListener(mJpegCallback));

  }

  private OnClickListener createOnClickListener(final PictureCallback callback) {
    return new OnClickListener() {
      @Override
      public void onClick(View v) {
        if (preview.hasCameraEnabled()) {
          preview.camera.takePicture(shutterCallback, rawCallback, callback);
        }
        else {
          showNoCameraError();
        }
      }
    };
  }

  public static void showNoCameraError() {
    popup(R.string.no_camera_error);
  }

  // Called when shutter is opened
  ShutterCallback shutterCallback = new ShutterCallback() {
    @Override
    public void onShutter() {

    }
  };

  // Handles data for raw picture
  PictureCallback rawCallback = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {

    }
  };

  // Handles data for jpeg picture "ton equipe"
  PictureCallback tJpegCallback = new TeamPictureCallback(Team.YOUR_TEAM);
  // Handles data for jpeg picture "mon equipe"
  PictureCallback mJpegCallback = new TeamPictureCallback(Team.MY_TEAM);

  private class TeamPictureCallback implements PictureCallback {

    private Team team;

    public TeamPictureCallback(Team team) {
      this.team = team;
    }

    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
      EquipePictureCallback callBack = new EquipePictureCallback(this.team, getBitmap(team));
      callBack.onPictureTaken(data, camera);
      imagePath = callBack.getPath();
      if (conn != null) {
        conn.disconnect();
      }
      conn = new MediaScannerConnection(CameraActivity.this, CameraActivity.this);
      conn.connect();
      Intent intent = new Intent();
      intent.setAction(android.content.Intent.ACTION_VIEW);
      intent.setDataAndType(Uri.fromFile(new File(callBack.getPath())), "image/jpeg");
      startActivity(intent);
    }
  };

  public static Context getContext() {
    return context;
  }

  public Bitmap getBitmap(Team team) {
    switch (team) {
      case MY_TEAM:
        return monEquipeBmp;
      case YOUR_TEAM:
        return tonEquipeBmp;
      default:
        return tonEquipeBmp;
    }
  }

  @Override
  public void onDestroy() {
    super.onDestroy();
    if (preview.camera != null) {
      preview.camera.release();
    }
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity, menu);
    return super.onCreateOptionsMenu(menu);
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
      case R.id.about:
        createAboutAlertDialog();
        break;
      case R.id.my_team:
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        File directory = new File(picFolder + Team.MY_TEAM.getDirectory());
        File[] files = directory.listFiles();
        if (files == null || files.length == 0) {
          popup(R.string.my_team_empty);
        }
        else {
          intent.setDataAndType(Uri.fromFile(files[files.length - 1]), "image/jpeg");
          startActivity(intent);
        }
        break;
      case R.id.your_team:
        intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        directory = new File(picFolder + Team.YOUR_TEAM.getDirectory());
        files = directory.listFiles();
        if (files == null || files.length == 0) {
          popup(R.string.your_team_empty);
        }
        else {
          intent.setDataAndType(Uri.fromFile(files[files.length - 1]), "image/jpeg");
          startActivity(intent);
        }
        break;
    }
    return super.onOptionsItemSelected(menuItem);
  }

  private void createAboutAlertDialog() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(R.string.about_message).setTitle(R.string.about);
    builder.setNegativeButton("OK", new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {

      }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
  }

  public static List<SimpleCamera> getCameraList() {
    return cameraInfo;
  }

  public static void popup(int assetId) {
    popup(getContext().getResources().getString(assetId), Toast.LENGTH_LONG);
  }

  public static void popup(int assetId, int toastLength) {
    popup(getContext().getResources().getString(assetId), toastLength);
  }

  public static void popup(String message) {
    popup(message, Toast.LENGTH_LONG);
  }

  public static void popup(String message, int toastLength) {
    Toast.makeText(getContext(), message, toastLength).show();
  }

  public static Camera getCamera() {
    return preview.camera;
  }

  @Override
  public void onMediaScannerConnected() {
    try {
      conn.scanFile(imagePath, "image/*");
    }
    catch (java.lang.IllegalStateException e) {

    }
  }

  @Override
  public void onScanCompleted(String path, Uri uri) {
    conn.disconnect();
  }

}




Java Source Code List

com.selesse.apps.EquipePictureCallback.java
com.selesse.apps.Preview.java
com.selesse.apps.activity.CameraActivity.java
com.selesse.apps.model.SimpleCamera.java
com.selesse.apps.model.Team.java