I am trying to save my onMapClick intent (the point where the user taps on the map to place a marker, however in my app i take a picture first then return) But it seems on Activity for result destroys or "forget" if you will to reutn the image, so i am trying to implement onSaveInstanceState
and onRestoreInstanceState
. But this doesn't seem to work.
Here is my code i have so far:
private static Uri fileUri;
private static final int TAKE_PICTURE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_googlemaps);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("TAKE_PICTURE", 1);
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getInt("resultCode", 1);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
And:
public void onMapLongClick(LatLng point) {
root = Environment.getExternalStorageDirectory().toString()
+ "/Your_Folder";
imageFolderPath = root + "/saved_images";
File imagesFolder = new File(imageFolderPath);
imagesFolder.mkdirs();
imageName = "test.png";
File image = new File(imageFolderPath, imageName);
fileUri = Uri.fromFile(image);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent,
TAKE_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data, LatLng point) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
try {
GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
bitmap = getImageThumbnail.getThumbnail(fileUri, this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
MarkerOptions markerOptions = new MarkerOptions()
.position(point)
.icon(BitmapDescriptorFactory
.fromBitmap(bitmap));
googleMap.addMarker(markerOptions);
}
}
public void showFullImage(View view) {
String path = (String) view.getTag();
if (path != null) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + path);
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
}
}
Am I doing this wrong? Please help
FULL CODE
public class Test extends Activity implements OnMapLongClickListener, OnMapClickListener{
private GoogleMap googleMap;
static final LatLng Location = new LatLng(xx.xxxx, xx.xxxx);
private static double latitude;
private static double longitude;
static final LatLng point = new LatLng(latitude /1E6, longitude /1E6);
MarkerOptions markerOptions;
Bitmap bitmap;
private String root;
private String imageFolderPath;
private String imageName;
private static Uri fileUri;
private static final int TAKE_PICTURE = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_googlemaps);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putInt("TAKE_PICTURE", 1);
// etc.
}
@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
savedInstanceState.getInt("resultCode", 1);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* function to load map. If map is not created it will create it for you
* */
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
if (googleMap!=null){
googleMap.addMarker(new MarkerOptions().position(Location)
.title("Location 1")
.icon(BitmapDescriptorFactory.fromResource(R.drawable.campmarker)));
}
CameraPosition cameraPosition = new CameraPosition.Builder().target(
new LatLng(xx.xxxx, xx.xxxx)).zoom(8).bearing(0).tilt(80).build();
googleMap.setOnMapClickListener(this);
googleMap.setOnMapLongClickListener(this);
googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
// adding marker
googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
googleMap.setMyLocationEnabled(true); // false to disable
googleMap.getUiSettings().setZoomControlsEnabled(false); // true to enable
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setMyLocationButtonEnabled(true);
}
public void onMapLongClick(LatLng point) {
root = Environment.getExternalStorageDirectory().toString()
+ "/My Folder";
imageFolderPath = root + "/saved_images";
File imagesFolder = new File(imageFolderPath);
imagesFolder.mkdirs();
imageName = "test.png";
File image = new File(imageFolderPath, imageName);
fileUri = Uri.fromFile(image);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(takePictureIntent,
TAKE_PICTURE);
}
public void onActivityResult(int requestCode, int resultCode, Intent data, LatLng point) {
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
try {
GetImageThumbnail getImageThumbnail = new GetImageThumbnail();
bitmap = getImageThumbnail.getThumbnail(fileUri, this);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
MarkerOptions markerOptions = new MarkerOptions()
.position(point)
.icon(BitmapDescriptorFactory
.fromBitmap(bitmap));
googleMap.addMarker(markerOptions);
}
}
public void showFullImage(View view) {
String path = (String) view.getTag();
if (path != null) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + path);
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
}
}