I'm doing an Android App in which I take a photo with camera, then there is A Gallery (of the photos), under an ImageView (the photo in format of Bitmap), and textviews in which I say name of photo (date), and position (latitude and longitude, with GPS). Obviously, if I click in an Image of the gallery, it'll appear in full screen! My problems are: 1. How to connect gallery with ImageViews (bitmap) by Camera. 2. How do I "save" the gallery? (Maybe with Hashmap, but I don't know what's that!)...Please don't make complicated codes cause this isn't an advanced android exam! Sorry for my terrible english!! This is my code:`
package com.example.app1;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mImageView=(ImageView) findViewById(R.id.foto);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
private void setPic() {
// Get the dimensions of the View
int targetW = mImageView.getWidth();
int targetH = mImageView.getHeight();
// Get the dimensions of the bitmap
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
bmOptions.inPurgeable = true;
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
mImageView.setImageBitmap(bitmap);
}
//GPS
private String providerId = LocationManager.GPS_PROVIDER;
private Geocoder geo = null;
private LocationManager locationManager=null;
private static final int MIN_DIST=20;
private static final int MIN_PERIOD=30000;
/*onLocationChanged è il cuore del listener e viene
invocato ogni volta che nuove informazioni di posizione sono state recapitate.*/
private LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
updateGUI(location);
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub}
};
/*L’oggetto Location contiene tutto ciò che è stato appreso dall’ultima misurazione del posizionamento e
viene inviata al metodo updateGUI per riflettere gli aggiornamenti sulla interfaccia utente: */
private void updateGUI(Location location)
{
Date timestamp = new Date(location.getTime()); //ecco dove si trova la
updateText(R.id.timestamp, timestamp.toString()); //data (nome della foto!!)
double latitude = location.getLatitude();
updateText(R.id.latitude, String.valueOf(latitude));
double longitude = location.getLongitude();
updateText(R.id.longitude, String.valueOf(longitude));
new AddressSolver().execute(location);
}
private void updateText(int id, String text)
{
TextView textView = (TextView) findViewById(id);
textView.setText(text);
}
private class AddressSolver extends AsyncTask<Location, Void, String>
{
@Override
protected String doInBackground(Location... params)
{
Location pos=params[0];
double latitude = pos.getLatitude();
double longitude = pos.getLongitude();
List<Address> addresses = null;
try
{
addresses = geo.getFromLocation(latitude, longitude, 1);
}
catch (IOException e)
{
}
if (addresses!=null)
{
if (addresses.isEmpty()) {
return null;
}
else {
if (addresses.size() > 0)
{
StringBuffer address=new StringBuffer();
Address tmp=addresses.get(0);
for (int y=0;y<tmp.getMaxAddressLineIndex();y++)
address.append(tmp.getAddressLine(y)+"\n");
return address.toString();
}
}
}
return null;
}
}
@Override
protected void onResume()
{
super.onResume();
geo=new Geocoder(this, Locale.getDefault());
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location!=null)
updateGUI(location);
locationManager.requestLocationUpdates(providerId, MIN_PERIOD,MIN_DIST, locationListener);
}
@Override
protected void onPause()
{
super.onPause();
if (locationManager!=null && locationManager.isProviderEnabled(providerId))
locationManager.removeUpdates(locationListener);
}
}
And this is my layout (activity_main.xml):
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:tools="http://ift.tt/LrGmb4"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.app1.MainActivity" >
<Gallery android:id="@+id/gallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"/>
<ImageView
android:id="@+id/foto"
android:layout_width="fill_parent"
android:layout_height="450sp" />
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TableRow android:padding="5dp">
<TextView android:text="Nome foto:" android:padding="5dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="@+id/timestamp" android:padding="5dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</TableRow>
<TableRow android:padding="5dp">
<TextView android:text="Latitudine:" android:padding="5dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="@+id/latitude" android:padding="5dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</TableRow>
<TableRow android:padding="5dp">
<TextView android:text="Longitudine:" android:padding="5dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="@+id/longitude" android:padding="5dp"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
</TableRow>
</TableLayout>
</LinearLayout>