Guest User

Untitled

a guest
Jul 18th, 2014
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.33 KB | None | 0 0
  1. package com.example.instareport;
  2.  
  3. import java.io.File;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import android.annotation.SuppressLint;
  7. import android.app.Activity;
  8. import android.content.ContentValues;
  9. import android.content.Context;
  10. import android.content.Intent;
  11. import android.database.Cursor;
  12. import android.graphics.Bitmap;
  13. import android.graphics.drawable.BitmapDrawable;
  14. import android.net.Uri;
  15. import android.os.Bundle;
  16. import android.os.Environment;
  17. import android.provider.MediaStore;
  18. import android.provider.MediaStore.Images;
  19. import android.provider.MediaStore.MediaColumns;
  20. import android.util.Log;
  21. import android.view.Menu;
  22. import android.view.MenuItem;
  23. import android.widget.ImageView;
  24.  
  25. /* This class is responsible for
  26.  MainActivity activity.
  27.  
  28.  First phase:
  29.  The 'MainActivity' is responsible for
  30.  the first screen to appear.
  31.  The main logic is, when the user presses
  32.  the app, MainActivity should now
  33.  start the Camera application,
  34.  and then when the user start to press the
  35.  shutter the image is then passed as an
  36.  intent towards the Main Activity.
  37.  
  38.  Second phase:
  39.  Once the image is received by the
  40.  MainActivity, it is now passed towards
  41.  the printer server
  42.  
  43.  Third phase:
  44.  The pdf file is composed of:
  45.  1.) image bitmap
  46.  2.) gps coordinates
  47.  3.) text
  48.  
  49.  Fourth phase:
  50.  Raspberry Pi front end
  51.  
  52.  */
  53.  
  54. public class MainActivity extends Activity {
  55.  
  56.     static final int REQUEST_IMAGE_CAPTURE = 1;
  57.     private ImageView mImageView;
  58.  
  59.     private static String imageFilePath;
  60.     private static Uri fileUri;
  61.  
  62.     // private Bitmap imageBitmap;
  63.  
  64.     private void dispatchTakePictureIntent() {
  65.         Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  66.         if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
  67.             fileUri = getOutputMediaFileUri();
  68.             takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
  69.             startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
  70.             ;
  71.         }
  72.     }
  73.  
  74.     @Override
  75.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  76.         if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
  77.             super.onActivityResult(requestCode, resultCode, data);
  78.             addImageToGallery(imageFilePath, getApplicationContext());
  79.             // imageBitmap = (Bitmap) data.getExtras().get("data");
  80.             Bitmap b = new BitmapDrawable(getApplicationContext()
  81.                     .getResources(), imageFilePath).getBitmap();
  82.             mImageView.setImageBitmap(b);
  83.         }
  84.     }
  85.  
  86.     @Override
  87.     protected void onCreate(Bundle savedInstanceState) {
  88.         super.onCreate(savedInstanceState);
  89.         setContentView(R.layout.activity_main);
  90.         mImageView = (ImageView) findViewById(R.id.imageView1);
  91.         dispatchTakePictureIntent();
  92.     }
  93.  
  94.     @Override
  95.     public boolean onCreateOptionsMenu(Menu menu) {
  96.  
  97.         // Inflate the menu; this adds items to the action bar if it is present.
  98.         getMenuInflater().inflate(R.menu.main, menu);
  99.         return true;
  100.     }
  101.  
  102.     @Override
  103.     public boolean onOptionsItemSelected(MenuItem item) {
  104.         // Handle action bar item clicks here. The action bar will
  105.         // automatically handle clicks on the Home/Up button, so long
  106.         // as you specify a parent activity in AndroidManifest.xml.
  107.         int id = item.getItemId();
  108.         if (id == R.id.action_settings) {
  109.             return true;
  110.         }
  111.         return super.onOptionsItemSelected(item);
  112.     }
  113.  
  114.     public String getPath(Uri uri) {
  115.         String[] projection = { MediaColumns.DATA };
  116.         Cursor cursor = getContentResolver().query(uri, projection, null, null,
  117.                 null);
  118.         if (cursor != null) {
  119.             cursor.moveToFirst();
  120.             int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
  121.             String filePath = cursor.getString(columnIndex);
  122.             cursor.close();
  123.             return filePath;
  124.         } else
  125.             return uri.getPath();
  126.  
  127.     }
  128.  
  129.     /** Create a file Uri for saving an image or video */
  130.     private static Uri getOutputMediaFileUri() {
  131.         return Uri.fromFile(getOutputMediaFile());
  132.     }
  133.  
  134.     /** Create a File for saving an image or video */
  135.     @SuppressLint("SimpleDateFormat")
  136.     private static File getOutputMediaFile() {
  137.         // To be safe, you should check that the SDCard is mounted
  138.         // using Environment.getExternalStorageState() before doing this.
  139.  
  140.         File mediaStorageDir = new File(
  141.                 Environment
  142.                         .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
  143.                 "instaReport");
  144.         // This location works best if you want the created images to be shared
  145.         // between applications and persist after your app has been uninstalled.
  146.  
  147.         // Create the storage directory if it does not exist
  148.         if (!mediaStorageDir.exists()) {
  149.             if (!mediaStorageDir.mkdirs()) {
  150.                 Log.d("instaReport", "failed to create directory");
  151.                 return null;
  152.             }
  153.         }
  154.  
  155.         // Create a media file name
  156.         String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
  157.                 .format(new Date());
  158.         File mediaFile;
  159.         imageFilePath = mediaStorageDir.getPath() + File.separator + "IMG_"
  160.                 + timeStamp + ".jpg";
  161.         mediaFile = new File(imageFilePath);
  162.  
  163.         return mediaFile;
  164.     }
  165.  
  166.     public static void addImageToGallery(final String filePath,
  167.             final Context context) {
  168.  
  169.         ContentValues values = new ContentValues();
  170.  
  171.         values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
  172.         values.put(Images.Media.MIME_TYPE, "image/jpeg");
  173.         values.put(MediaStore.MediaColumns.DATA, filePath);
  174.  
  175.         context.getContentResolver().insert(Images.Media.EXTERNAL_CONTENT_URI,
  176.                 values);
  177.     }
  178.  
  179. }
Advertisement
Add Comment
Please, Sign In to add comment