Skip to content

Instantly share code, notes, and snippets.

@BrianJVarley
Created January 1, 2015 21:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrianJVarley/cc2a05703aea7f69b50c to your computer and use it in GitHub Desktop.
Save BrianJVarley/cc2a05703aea7f69b50c to your computer and use it in GitHub Desktop.
package ie.gmit.computing;
//import com.example.cameratest.R;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
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.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.media.ExifInterface;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener,LocationListener {
private static final String TAG = "CallCamera";
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Uri fileUri = null;
ImageView photoImage = null;
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photoImage = (ImageView) findViewById(R.id.photo_image);
photoImage.setImageDrawable(null);
Button mClickButton1 = (Button)findViewById(R.id.cameraBtn);
mClickButton1.setOnClickListener(this);
Button mClickButton2 = (Button)findViewById(R.id.searchBtn);
mClickButton2.setOnClickListener(this);
Button mClickButton3 = (Button)findViewById(R.id.resBtn);
mClickButton3.setOnClickListener(this);
latituteField = (TextView) findViewById(R.id.latitude_text);
longitudeField = (TextView) findViewById(R.id.longitude_text);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField.setText("Location not available");
longitudeField.setText("Location not available");
}
}
//handle button clicks
public void onClick(View v) {
switch (v.getId()) {
case R.id.cameraBtn: {
// start camera intent
Toast.makeText(this, "camera clicked", Toast.LENGTH_SHORT).show();
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
File file = getOutputPhotoFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
fileUri = Uri.fromFile(getOutputPhotoFile());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
i.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
break;
}
case R.id.resBtn: {
// start camera intent
Toast.makeText(this, "result clicked", Toast.LENGTH_SHORT).show();
Intent myIntentO = new Intent(MainActivity.this, SearchResult.class);
MainActivity.this.startActivity(myIntentO);
break;
}
case R.id.searchBtn: {
// search tree for matching debri
Toast.makeText(this, "search clicked", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, SearchTree.class);
MainActivity.this.startActivity(myIntent);
break;
}
}
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Toast.makeText(this, "Add", Toast.LENGTH_SHORT).show();
return true;
case R.id.delete:
Toast.makeText(this, "Delete", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_settings:
Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show();
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Generic Info");
alert.setMessage("Ship Name");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
Editable value = input.getText();
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private File getOutputPhotoFile() throws IOException {
File directory = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), getPackageName());
if (!directory.exists()) {
if (!directory.mkdirs()) {
Log.e(TAG, "Failed to create storage directory.");
return null;
}
}
/*
String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss", Locale.ENGLISH).format(new Date());
File exifVar = new File(directory.getPath(), "IMG_" + timeStamp + ".jpg");
*/
File[] files = directory.listFiles();
if(files.length==0) {
// No images available
return new File("") ;
}
File newestFile = files[files.length-1];
File exifVar = new File(directory.getPath() + File.separator + newestFile.getName());
String mString = "Generic Text..";
ExifInterface exif = new ExifInterface(exifVar.getAbsolutePath());
exif.setAttribute("UserComment", mString);
exif.saveAttributes();
exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE,
String.valueOf(latituteField.toString()));
exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE,
String.valueOf(longitudeField.toString()));
exif.saveAttributes();
return exifVar;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Uri photoUri = null;
if (data == null) {
Toast.makeText(this, "Image saved successfully",
Toast.LENGTH_LONG).show();
photoUri = fileUri;
} else {
photoUri = data.getData();
Toast.makeText(this, "Image saved successfully in: " + data.getData(),
Toast.LENGTH_LONG).show();
}
showPhoto(photoUri);
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Cancelled", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Callout for image capture failed!",
Toast.LENGTH_LONG).show();
}
}
}
private void showPhoto(Uri photoUri) {
String filePath = photoUri.getEncodedPath();
File imageFile = new File(filePath);
if (imageFile.exists()){
Drawable oldDrawable = photoImage.getDrawable();
if (oldDrawable != null) {
((BitmapDrawable)oldDrawable).getBitmap().recycle();
}
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
BitmapDrawable drawable = new BitmapDrawable(this.getResources(), bitmap);
photoImage.setScaleType(ImageView.ScaleType.FIT_CENTER);
photoImage.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 130, 110, false));
}
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
latituteField.setText("Lat: " + String.valueOf(lat));
longitudeField.setText("Lon: " + String.valueOf(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment