如何保存onMapClick意图,一旦活动回报恢复 [英] How to save the onMapClick intent and restore once activity returns

查看:174
本文介绍了如何保存onMapClick意图,一旦活动回报恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想救我onMapClick意图(用户水龙头在地图上放置一个标记点​​,但在我的应用我照张相第一,然后返回),但似乎对结果的活动破坏或忘记如果你到reutn形象,所以我想实现的onSaveInstanceState onRestoreInstanceState 。但是,这似乎并没有工作。

下面是我的code我到目前为止有:

 私有静态乌里了fileURI;
  私有静态最终诠释TAKE_PICTURE = 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.fragment_googlemaps);
}
@覆盖
公共无效的onSaveInstanceState(捆绑savedInstanceState){
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putInt(TAKE_PICTURE,1);
  //等等。
}
@覆盖
公共无效onRestoreInstanceState(捆绑savedInstanceState){
          super.onRestoreInstanceState(savedInstanceState);
          savedInstanceState.getInt(结果code,1);
    尝试{
        //载入地图
        initilizeMap();    }赶上(例外五){
        e.printStackTrace();
    }
    }

 公共无效onMapLongClick(经纬度点){
        根= Environment.getExternalStorageDirectory()。的toString()
           +/您的文件夹;
           imageFolderPath =根+/ saved_images;
           文件imagesFolder =新的文件(imageFolderPath);
           imagesFolder.mkdirs();
          imageName =test.png;
          文件图像=新的文件(imageFolderPath,imageName);          了fileURI = Uri.fromFile(图片);
          意图takePictureIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,fileURI所);          startActivityForResult(takePictureIntent,
                  拍照片);
  }       公共无效的onActivityResult(INT申请code,INT结果code,意向数据,经纬度点){
          如果(要求code == TAKE_PICTURE&放大器;&安培;结果code == RESULT_OK){                  尝试{
                      GetImageThumbnail getImageThumbnail =新GetImageThumbnail();
                      位= getImageThumbnail.getThumbnail(fileURI所,这一点);
                  }赶上(FileNotFoundException异常E1){
                      e1.printStackTrace();
                  }赶上(IOException异常E1){
                      e1.printStackTrace();
                  }                  的MarkerOptions的MarkerOptions =新的MarkerOptions()
                  .POSITION(点)
                  .icon(BitmapDesc​​riptorFactory
                  .fromBitmap(位图));
                  googleMap.addMarker(的MarkerOptions);
          }
          }         公共无效showFullImage(查看视图){
            字符串路径=(字符串)view.getTag();            如果(路径!= NULL){                意向意图=新的Intent();
                intent.setAction(Intent.ACTION_VIEW);
                乌里imgUri = Uri.parse(文件://+路径);
                intent.setDataAndType(imgUri,图像/ *);
                startActivity(意向);
            }            }

我这样做不对吗?
请帮助

FULL code

 公共类测试扩展活动实现OnMapLongClickListener,OnMapClickListener {
私人GoogleMap的GoogleMap的;
静态最终经纬度位置=新的经纬度(xx.xxxx,xx.xxxx);私有静态双纬度;
私有静态双经度;
静态最终经纬度点=新的经纬度(纬度/ 1E6,经度/ 1E6);
的MarkerOptions的MarkerOptions;  位图位图;
  私人串根;
  私人字符串imageFolderPath;
  私人字符串imageName;  私有静态乌里了fileURI;
  私有静态最终诠释TAKE_PICTURE = 0;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.fragment_googlemaps);
}
@覆盖
公共无效的onSaveInstanceState(捆绑savedInstanceState){
  super.onSaveInstanceState(savedInstanceState);
  savedInstanceState.putInt(TAKE_PICTURE,1);
  //等等。
}
@覆盖
公共无效onRestoreInstanceState(捆绑savedInstanceState){
          super.onRestoreInstanceState(savedInstanceState);
          savedInstanceState.getInt(结果code,1);
    尝试{
        //载入地图
        initilizeMap();    }赶上(例外五){
        e.printStackTrace();
    }
    }/ **
 *函数来加载地图。如果地图不创建它会为您创建它
 * * /
私人无效initilizeMap(){
    如果(GoogleMap的== NULL){
        使用GoogleMap =((MapFragment)getFragmentManager()findFragmentById(R.id.map)。)的GetMap();        //检查是否映射成功与否创建
        如果(GoogleMap的== NULL){
            Toast.makeText(getApplicationContext(),
                    对不起!无法创建地图,Toast.LENGTH_SHORT)
                    。显示();
        }
    }
 }
 @覆盖
   公共无效onResume(){
    super.onResume();
    initilizeMap();    如果(GoogleMap的!= NULL){
        googleMap.addMarker(新的MarkerOptions()位置(位置)
            .title伪(位置1)
            .icon(BitmapDesc​​riptorFactory.fromResource(R.drawable.campmarker)));      }
 CameraPosition cameraPosition =新CameraPosition.Builder()。目标(
         新经纬度(xx.xxxx,xx.xxxx))放大(8).bearing(0).tilt(80).build(); googleMap.setOnMapClickListener(本);
 googleMap.setOnMapLongClickListener(本);
 googleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); //添加标记 googleMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
 googleMap.setMyLocationEnabled(真); //为false表示禁用
 googleMap.getUiSettings()setZoomControlsEnabled(假)。 // true将启用
 googleMap.getUiSettings()setCompassEnabled(真)。
 googleMap.getUiSettings()setMyLocationButtonEnabled(真)。
}  公共无效onMapLongClick(经纬度点){
        根= Environment.getExternalStorageDirectory()。的toString()
           +/我的文件夹;
           imageFolderPath =根+/ saved_images;
           文件imagesFolder =新的文件(imageFolderPath);
           imagesFolder.mkdirs();
          imageName =test.png;
          文件图像=新的文件(imageFolderPath,imageName);          了fileURI = Uri.fromFile(图片);
          意图takePictureIntent =新意图(MediaStore.ACTION_IM​​AGE_CAPTURE);          takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,fileURI所);          startActivityForResult(takePictureIntent,
                  拍照片);
  }       公共无效的onActivityResult(INT申请code,INT结果code,意向数据,经纬度点){
          如果(要求code == TAKE_PICTURE&放大器;&安培;结果code == RESULT_OK){                  尝试{
                      GetImageThumbnail getImageThumbnail =新GetImageThumbnail();
                      位= getImageThumbnail.getThumbnail(fileURI所,这一点);
                  }赶上(FileNotFoundException异常E1){
                      e1.printStackTrace();
                  }赶上(IOException异常E1){
                      e1.printStackTrace();
                  }                  的MarkerOptions的MarkerOptions =新的MarkerOptions()
                  .POSITION(点)
                  .icon(BitmapDesc​​riptorFactory
                  .fromBitmap(位图));
                  googleMap.addMarker(的MarkerOptions);
          }
          }         公共无效showFullImage(查看视图){
            字符串路径=(字符串)view.getTag();            如果(路径!= NULL){                意向意图=新的Intent();
                intent.setAction(Intent.ACTION_VIEW);
                乌里imgUri = Uri.parse(文件://+路径);
                intent.setDataAndType(imgUri,图像/ *);
                startActivity(意向);
            }            }


解决方案

的问题和意见是间preT非常困难。我要承担这一切的东西归结为:


  

我如何守住经纬度传递到 onMapLongClick(),因为拍照是造成被终止,并重新启动我的过程吗?


首先,经纬度数据成员添加到您的活动。对于这个答案的目的,我将其称之为:

 经纬度theLastPlaceThatTheUserLongClicked;

onMapLongClick(),作为第一行,保存关闭经纬度传递到 onMapLongClick()

  theLastPlaceThatTheUserLongClicked =点;

在你的类创建另一个常量:

 私有静态最后弦乐EXTRA_PLACE =theLastPlaceThatTheUserLongClicked;

替换现有的的onSaveInstanceState() onRestoreInstanceState()

  @覆盖
公共无效的onSaveInstanceState(捆绑savedInstanceState){
  super.onSaveInstanceState(savedInstanceState);  savedInstanceState.putParcelable(EXTRA_PLACE,theLastPlaceThatTheUserLongClicked);
}@覆盖
公共无效onRestoreInstanceState(捆绑savedInstanceState){
  super.onRestoreInstanceState(savedInstanceState);  theLastPlaceThatTheUserLongClicked =(经纬度)savedInstanceState.getParcelable(EXTRA_PLACE);
}

然后,当你需要的最后的地方,用户长点击,使用 theLastPlaceThatTheUserLongClicked

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);
            }

            }

解决方案

The question and comments are very difficult to interpret. I am going to assume that all of this stuff boils down to:

How do I hold onto the LatLng that is passed into onMapLongClick(), given that taking a picture is causing my process to be terminated and restarted?

First, add a LatLng data member to your activity. For the purposes of this answer, I will call it:

LatLng theLastPlaceThatTheUserLongClicked;

In onMapLongClick(), as the very first line, save off the LatLng that is passed into onMapLongClick():

theLastPlaceThatTheUserLongClicked=point;

Create another constant in your class:

private static final String EXTRA_PLACE="theLastPlaceThatTheUserLongClicked";

Replace your existing onSaveInstanceState() and onRestoreInstanceState() with:

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
  super.onSaveInstanceState(savedInstanceState);

  savedInstanceState.putParcelable(EXTRA_PLACE, theLastPlaceThatTheUserLongClicked);
}   

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);

  theLastPlaceThatTheUserLongClicked=(LatLng)savedInstanceState.getParcelable(EXTRA_PLACE);
}

Then, when you need the last place that the user long-clicked, use theLastPlaceThatTheUserLongClicked.

这篇关于如何保存onMapClick意图,一旦活动回报恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆