I am creating an app in which a user can create events that are happening on campus. I would like to have a way for a user to select a location on a map on a button push. The button would open an Google Maps intent which the user could then place a marker or drag one to their desired location. Once selected the coordinates would return as a pair of doubles. I will be then storing these in a database which displays the points on another activity.
I need to know how to select a location on a map intent and return the selected coordinates. Here is the code for this activity. I have removed some of the extraneous methods for date picking and such.
public class CreateEvent extends Activity {
EditText startTimeField;
EditText endTimeField;
EditText startDateField;
EditText endDateField;
EditText locationField;
Calendar startTime;
Calendar endTime;
ImageView mImageView;
Bitmap imageBitmap;
Button capture;
Button locationButton;
static final int REQUEST_IMAGE_CAPTURE = 1;
SimpleDateFormat time_format = new SimpleDateFormat("hh:mm a", Locale.US);
SimpleDateFormat date_format = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_event);
startTime = new GregorianCalendar();
endTime = new GregorianCalendar();
endTime.set(Calendar.HOUR, endTime.get(Calendar.HOUR) + 1);
startTimeField = (EditText)this.findViewById(R.id.startTimeField);
endTimeField = (EditText)this.findViewById(R.id.endTimeField);
startDateField = (EditText)this.findViewById(R.id.startDateField);
endDateField = (EditText)this.findViewById(R.id.endDateField);
locationField = (EditText) this.findViewById(R.id.locationField);
startTimeField.setKeyListener(null);
endTimeField.setKeyListener(null);
startDateField.setKeyListener(null);
endDateField.setKeyListener(null);
startTimeField.setText(time_format.format(startTime.getTime()));
endTimeField.setText(time_format.format(endTime.getTime()));
startDateField.setText(date_format.format(startTime.getTime()));
endDateField.setText(date_format.format(endTime.getTime()));
mImageView = (ImageView) this.findViewById(R.id.eventPhoto);
if(savedInstanceState != null){
imageBitmap = savedInstanceState.getParcelable("eventPhoto");
if(imageBitmap != null){
mImageView.setImageBitmap(imageBitmap);
}
}
capture = (Button) findViewById(R.id.photoButton);
capture.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
});
locationButton = (Button) findViewById(R.id.locationButton);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
#Override
public void onSaveInstanceState(Bundle outstate){
outstate.putParcelable("eventPhoto", imageBitmap);
super.onSaveInstanceState(outstate);
public void makeEvent(View view){
Intent intent = new Intent();
intent.putExtra("Event", new Event(imageBitmap, ((EditText)findViewById(R.id.eventField)).getText().toString(),
((EditText)findViewById(R.id.locationField)).getText().toString(), startTime.getTime(),
endTime.getTime(), ((EditText)findViewById(R.id.descriptionField)).getText().toString()));
// intent.putExtra("Bitmap", );
// intent.putExtra("Name", );
// intent.putExtra("Location", );
// intent.putExtra("Start", );
// intent.putExtra("End", );
// intent.putExtra("Description", );
setResult(RESULT_OK, intent);
finish();
}
}
All other questions regarding this have very outdated answers. Thanks for any help.
EDIT
Here is the code I am currently using for the intent:
locationButton = (Button) findViewById(R.id.locationButton);
locationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Uri gmmIntentUri = Uri.parse("geo:42.273856,-71.805976?z=16");
Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
mapIntent.setPackage("com.google.android.apps.maps");
if (mapIntent.resolveActivity(getPackageManager()) != null) {
startActivity(mapIntent);
}
}});
Related
I've been trying to pass the image from gallery or from camera to another activity but they don't work properly. If the image from the gallery will work then the image from camera won't work and vice versa. Please help me fix my code. Here are my code for the MainActivity and for the SecondActivity. Thank you!
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button btnStart,btnGallery,btnAbout;
ImageView reademlogo, pianotiles,imgclouds,imgclouds2,imgsharp,imgfclef;
TextView txtread;
Typeface tf1;
private final int REQUEST_IMAGE_CAPTURE = 1;
private final int PICK_IMAGE = 1;
Uri imageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
UserInterface();
}
private void UserInterface() {
btnStart = (Button)findViewById(R.id.btnStart);
btnGallery = (Button)findViewById(R.id.btnGallery);
btnAbout = (Button)findViewById(R.id.btnAbout);
reademlogo = (ImageView)findViewById(R.id.reademLogo);
pianotiles = (ImageView)findViewById(R.id.pianotiles);
imgclouds = (ImageView)findViewById(R.id.imgclouds);
imgclouds2 = (ImageView)findViewById(R.id.imgclouds2);
imgsharp = (ImageView)findViewById(R.id.imgsharp);
imgfclef = (ImageView)findViewById(R.id.imgfclef);
txtread = (TextView)findViewById(R.id.txtread);
tf1 = Typeface.createFromAsset(getAssets(),
"fonts/VeganStylPersonalUse.ttf");
txtread.setTypeface(tf1);
}
public void captureImage(View view) {
Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (iCamera.resolveActivity(getPackageManager()) != null) {
startActivityForResult(iCamera, REQUEST_IMAGE_CAPTURE);
}
}
public void galImage(View view) {
Intent iGallery = new Intent(Intent.ACTION_PICK,MediaStore.Images.Media.INTERNAL_CONTENT_URI);
iGallery.setType("image/*");
startActivityForResult(iGallery, PICK_IMAGE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK && requestCode == REQUEST_IMAGE_CAPTURE){
Bitmap bitmap = (Bitmap)Objects.requireNonNull(data.getExtras()).get("data");
Intent intent = new Intent(MainActivity.this,Camera.class);
intent.putExtra("Bitmap",bitmap);
startActivity(intent);
}
else if(requestCode == PICK_IMAGE){
if(data != null){
imageUri = data.getData();
Intent intent = new Intent(MainActivity.this,Camera.class);
intent.putExtra("imageUri",imageUri);
startActivity(intent);
}
}
}
}
Camera.java
public class Camera extends AppCompatActivity {
Button btnCap, btnUse;
ImageView imageView3;
Uri imageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
btnCap = (Button) findViewById(R.id.btnCap);
btnUse = (Button) findViewById(R.id.btnUse);
imageView3 = (ImageView) findViewById(R.id.imageView3);
if (getIntent().getExtras() != null) {
imageUri = Uri.parse(getIntent().getStringExtra("imageUri"));
imageView3.setImageURI(imageUri);
}
Intent intentCam = getIntent();
Bitmap camera_img = (Bitmap)intentCam.getParcelableExtra("Bitmap");
if(camera_img != null){
imageView3.setImageBitmap(camera_img);
}
}
}
Try this it is working fine for me.
//Use this method to select image from Gallery
private void processGalleryImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"),
GALLERY_REQUEST_CODE);
}
//Use this method to click image from Camera
private void processCameraImage() {
Intent cameraIntent = new
Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST_CODE);
}
//Use this method to get image from Gallery/Captured from Camera
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
if (requestCode == CAMERA_REQUEST_CODE) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
//Starting activity (ImageViewActivity in my code) to preview image
Intent intent = new Intent(this, ImageViewActivity.class);
intent.putExtra("BitmapImage", photo);
startActivity(intent);
} else if (requestCode == GALLERY_REQUEST_CODE) {
if (data.getData() != null) {
Uri imageUri = data.getData();
//Starting activity (ImageViewActivity in my code) to preview image
Intent intent = new Intent(this, ImageViewActivity.class);
intent.putExtra("ImageUri", imageUri.toString());
startActivity(intent);
}
}
}
}
//ImageViewActivity code
private Bitmap bitmap;
private String uri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_view);
ButterKnife.bind(this);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
bitmap = bundle.getParcelable("BitmapImage");
uri = bundle.getString("ImageUri");
if (bitmap != null)
imageView.setImageBitmap(bitmap);
else
Glide.with(this).load(uri).into(imageView);
}
}
I am using the Glide library to load image in image view in case if i have image url.
I am making an app that sends a picture taken from the Camera app however the image it returns seems to be only a thumbnail how can I get it to turn the whole image?
The following code gets an image, but it's too small.
public class OnTheJobActivity extends Activity{
private static final int CAMERA_PIC_REQUEST = 1337;
private Button takePictureButton;
private Button sendPictureButton;
private Bitmap thumbnail;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.onthejob);
takePictureButton = (Button) findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(takePictureButtonListener);
sendPictureButton = (Button) findViewById(R.id.sendPictureButton);
sendPictureButton.setOnClickListener(sendPictureButtonListener);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener takePictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
};
private OnClickListener sendPictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, "abc#gmail.com");
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
i.putExtra(Intent.EXTRA_STREAM, thumbnail);
i.setType("image/bmp");
startActivity(Intent.createChooser(i,"Emailfile"));
}
};
}
You could also change the intent you're using.
//in your buttonListener
ContentValues values = new ContentValues();
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
try{
startActivityForResult(i, ACTIVITY_GET_IMAGE);
}
catch(Exception ex){
Log.v("BRE", ex.toString());
}
//in your activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITY_GET_IMAGE){
if(resultCode == RESULT_OK){
try{String uri = data.getData().toString()}
catch(NullPointerException e){//do something}
}
}
}
This will return a uri which you can then use to access the full resolution image
Try using the implementation shown here
Specifically:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};
my program captures an image (as bitmap) but I don't know (and couldn't find) how to save it with a custom name at a custom path... Here's my image capture codes. Can I do it? Or is there any way saving it as another data type, not bitmap? some help will be great. Thanks.
public class ShowMessagesPage extends Activity {
final static int CAMERA_RESULT = 0;
DBAdapter db = new DBAdapter(this);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_messages_page);
Button userButton = (Button)findViewById(R.id.button1);
userButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
error();
}});
Button buttonPhoto = (Button)findViewById(R.id.buttonPhoto);
buttonPhoto.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAMERA_RESULT);
}});}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
Bundle extras = intent.getExtras();
Bitmap bmp = (Bitmap) extras.get("data");
}
}
and the other codes..........
Try this:
Intent getCameraImage = new Intent("android.media.action.IMAGE_CAPTURE");
File cameraFolder;
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
cameraFolder = new File(android.os.Environment.getExternalStorageDirectory(),"folder_name_of_your_choice");
else
cameraFolder= ShowMessagesPage.this.getCacheDir();
if(!cameraFolder.exists())
cameraFolder.mkdirs();
File photo = new File(Environment.getExternalStorageDirectory(), "folder_name_of_your_choice/camera_snap.jpg");
getCameraImage.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
initialURI = Uri.fromFile(photo);
startActivityForResult(getCameraImage, CAMERA_RESULT);
And in the onActivityResult(), process the Uri initialURI to get your Image which is already saved to the path you have chosen in the code posted above. The name of the image file will be: camera_snap.jpg
Note: The Uri initialURI is declared globally.
My app takes a picture using the phones camera and stores the picture in the phones gallery. I would like to then get that picture path and store it in my datastore. Can someone please help me? Heres my code:
public void onClick(View v){
switch(v.getId())
{
case R.id.btnLoadPic:
//Options for the dialogue menu
final CharSequence[] items = {"Camera", "Gallery"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose an Option");
builder.setItems(items, new DialogInterface.OnClickListener() {
/**
* Make onclick functionality for the options in the dialogue menu
*/
public void onClick(DialogInterface dialog, int item) {
// Camera option
if (item == 0){
PackageManager pm = getPackageManager();
if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)){
//Toast.makeText(this, "camera", Toast.LENGTH_SHORT).show();
dispatchTakePictureIntent(11);
} else {
Toast.makeText(null, "No camera avalible", Toast.LENGTH_SHORT).show();
}
}
// Gallery option this works fine
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
//handleSmallCameraPhoto(takePictureIntent);
}
private void handleSmallCameraPhoto(Intent intent) {
Bundle extras = intent.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
ImageView mImageView = (ImageView) this.findViewById(R.id.imagePlayer);
mImageView.setImageBitmap(mImageBitmap);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
handleSmallCameraPhoto(data);
}
The last bits of codes accesses the camera and displays the picture in the imageview. How do i get that picture path in a string format?
This will help. Tested and worked!
public class MainActivity extends Activity {
private static final int REQUEST_IMAGE = 100;
private static final String TAG = "MainActivity";
TextView tvPath;
ImageView picture;
File destination;
String imagePath;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvPath = (TextView) findViewById(R.id.idTvPath);
picture = (ImageView) findViewById(R.id.idIvImage);
String name = dateToString(new Date(),"yyyy-MM-dd-hh-mm-ss");
destination = new File(Environment.getExternalStorageDirectory(), name + ".jpg");
Button click = (Button) findViewById(R.id.idBtnTakePicture);
click.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destination));
startActivityForResult(intent, REQUEST_IMAGE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == REQUEST_IMAGE && resultCode == Activity.RESULT_OK ){
try {
FileInputStream in = new FileInputStream(destination);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 10;
imagePath = destination.getAbsolutePath();
tvPath.setText(imagePath);
Bitmap bmp = BitmapFactory.decodeStream(in, null, options);
picture.setImageBitmap(bmp);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
else{
tvPath.setText("Request cancelled");
}
}
public String dateToString(Date date, String format) {
SimpleDateFormat df = new SimpleDateFormat(format);
return df.format(date);
}
}
Dont forget to add these in your manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CAMERA"/>
I am making an app that sends a picture taken from the Camera app however the image it returns seems to be only a thumbnail how can I get it to turn the whole image?
The following code gets an image, but it's too small.
public class OnTheJobActivity extends Activity{
private static final int CAMERA_PIC_REQUEST = 1337;
private Button takePictureButton;
private Button sendPictureButton;
private Bitmap thumbnail;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.onthejob);
takePictureButton = (Button) findViewById(R.id.takePictureButton);
takePictureButton.setOnClickListener(takePictureButtonListener);
sendPictureButton = (Button) findViewById(R.id.sendPictureButton);
sendPictureButton.setOnClickListener(sendPictureButtonListener);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_PIC_REQUEST) {
thumbnail = (Bitmap) data.getExtras().get("data");
ImageView image = (ImageView) findViewById(R.id.photoResultView);
image.setImageBitmap(thumbnail);
sendPictureButton.setVisibility(Button.VISIBLE);
}
}
private OnClickListener takePictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
};
private OnClickListener sendPictureButtonListener = new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_EMAIL, "abc#gmail.com");
i.putExtra(Intent.EXTRA_SUBJECT,"On The Job");
i.putExtra(Intent.EXTRA_STREAM, thumbnail);
i.setType("image/bmp");
startActivity(Intent.createChooser(i,"Emailfile"));
}
};
}
You could also change the intent you're using.
//in your buttonListener
ContentValues values = new ContentValues();
imageUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
try{
startActivityForResult(i, ACTIVITY_GET_IMAGE);
}
catch(Exception ex){
Log.v("BRE", ex.toString());
}
//in your activity
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == ACTIVITY_GET_IMAGE){
if(resultCode == RESULT_OK){
try{String uri = data.getData().toString()}
catch(NullPointerException e){//do something}
}
}
}
This will return a uri which you can then use to access the full resolution image
Try using the implementation shown here
Specifically:
Camera.PictureCallback mPictureCallback = new Camera.PictureCallback() {
public void onPictureTaken(byte[] imageData, Camera c) {
}
};