Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.instareport;
- import java.io.File;
- import java.text.SimpleDateFormat;
- import java.util.Date;
- import android.annotation.SuppressLint;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Context;
- import android.content.Intent;
- import android.database.Cursor;
- import android.graphics.Bitmap;
- import android.graphics.drawable.BitmapDrawable;
- import android.net.Uri;
- import android.os.Bundle;
- import android.os.Environment;
- import android.provider.MediaStore;
- import android.provider.MediaStore.Images;
- import android.provider.MediaStore.MediaColumns;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.ImageView;
- /* This class is responsible for
- MainActivity activity.
- First phase:
- The 'MainActivity' is responsible for
- the first screen to appear.
- The main logic is, when the user presses
- the app, MainActivity should now
- start the Camera application,
- and then when the user start to press the
- shutter the image is then passed as an
- intent towards the Main Activity.
- Second phase:
- Once the image is received by the
- MainActivity, it is now passed towards
- the printer server
- Third phase:
- The pdf file is composed of:
- 1.) image bitmap
- 2.) gps coordinates
- 3.) text
- Fourth phase:
- Raspberry Pi front end
- */
- public class MainActivity extends Activity {
- static final int REQUEST_IMAGE_CAPTURE = 1;
- private ImageView mImageView;
- private static String imageFilePath;
- private static Uri fileUri;
- // private Bitmap imageBitmap;
- private void dispatchTakePictureIntent() {
- Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
- fileUri = getOutputMediaFileUri();
- takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
- startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
- ;
- }
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
- super.onActivityResult(requestCode, resultCode, data);
- addImageToGallery(imageFilePath, getApplicationContext());
- // imageBitmap = (Bitmap) data.getExtras().get("data");
- Bitmap b = new BitmapDrawable(getApplicationContext()
- .getResources(), imageFilePath).getBitmap();
- mImageView.setImageBitmap(b);
- }
- }
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- mImageView = (ImageView) findViewById(R.id.imageView1);
- dispatchTakePictureIntent();
- }
- @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 id = item.getItemId();
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- public String getPath(Uri uri) {
- String[] projection = { MediaColumns.DATA };
- Cursor cursor = getContentResolver().query(uri, projection, null, null,
- null);
- if (cursor != null) {
- cursor.moveToFirst();
- int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
- String filePath = cursor.getString(columnIndex);
- cursor.close();
- return filePath;
- } else
- return uri.getPath();
- }
- /** Create a file Uri for saving an image or video */
- private static Uri getOutputMediaFileUri() {
- return Uri.fromFile(getOutputMediaFile());
- }
- /** Create a File for saving an image or video */
- @SuppressLint("SimpleDateFormat")
- private static File getOutputMediaFile() {
- // To be safe, you should check that the SDCard is mounted
- // using Environment.getExternalStorageState() before doing this.
- File mediaStorageDir = new File(
- Environment
- .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
- "instaReport");
- // This location works best if you want the created images to be shared
- // between applications and persist after your app has been uninstalled.
- // Create the storage directory if it does not exist
- if (!mediaStorageDir.exists()) {
- if (!mediaStorageDir.mkdirs()) {
- Log.d("instaReport", "failed to create directory");
- return null;
- }
- }
- // Create a media file name
- String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
- .format(new Date());
- File mediaFile;
- imageFilePath = mediaStorageDir.getPath() + File.separator + "IMG_"
- + timeStamp + ".jpg";
- mediaFile = new File(imageFilePath);
- return mediaFile;
- }
- public static void addImageToGallery(final String filePath,
- final Context context) {
- ContentValues values = new ContentValues();
- values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
- values.put(Images.Media.MIME_TYPE, "image/jpeg");
- values.put(MediaStore.MediaColumns.DATA, filePath);
- context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
- values);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment