我正在尝试保存我的onMapClick意图(用户点击地图上放置标记的点,但是在我的应用程序中我首先拍照然后返回)但是它似乎在Activity上为结果破坏或“忘记”如果您要重新生成图片,那么我正在尝试实施onSaveInstanceState
和onRestoreInstanceState
。但这似乎不起作用。
这是我到目前为止的代码:
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();
}
}
和
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);
}
}
我这样做错了吗? 请帮忙
完整代码
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);
}
}
答案 0 :(得分:1)
问题和评论很难解释。我将假设所有这些都归结为:
我如何抓住传递到
LatLng
的{{1}},因为拍照导致我的进程被终止并重新启动?
首先,将onMapLongClick()
数据成员添加到您的活动中。出于本答案的目的,我将其称之为:
LatLng
在LatLng theLastPlaceThatTheUserLongClicked;
中,作为第一行,保存传递到onMapLongClick()
的{{1}}:
LatLng
在班级中创建另一个常量:
onMapLongClick()
将现有的theLastPlaceThatTheUserLongClicked=point;
和private static final String EXTRA_PLACE="theLastPlaceThatTheUserLongClicked";
替换为:
onSaveInstanceState()
然后,当您需要用户长按的最后一个位置时,请使用onRestoreInstanceState()
。