为保存在 SQLite 数据库中的标记加载图像

Loading image for Marker saved in SQlite Database

我已经使用 SQLite 数据库在我的应用程序中保存了标记 ID 和图像文件路径。我目前遇到的问题是,当再次加载地图时,标记会显示在用户添加它们的位置,但是通过相机意图(图像路径)为该标记拍摄的图像不会随标记一起加载。

所以要解释一下,我的标记有一个 CustomInfoWindow,它显示标题和片段(标记的位置),然后有一个 ImageView,它在放置标记后最初显示图像.但是当重新加载应用程序时,图像消失了,但显示了标题和片段。

所以在保存图像文件路径和标记ID时我使用:

contentValues.put(LocationsDB.FIELD_IMAGE, markerId);
contentValues.put(LocationsDB.FIELD_FILEPATH, image.getAbsolutePath());

它保存的数据是这样的:

m15  --> Marker ID
/storage/emulated/0/My Folder/MyImage_123.jpg --> File Path

然后在应用程序启动后加载标记时:

String filep = null;
String id = null;

id = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_IMAGE));
filep = arg1.getString(arg1.getColumnIndex(LocationsDB.FIELD_FILEPATH));

drawMarker(thePoint, title, snippet, id, filep);

那么在我的 drawMarker 方法中:

private void drawMarker(LatLng point, String title, String snippet, String id, String filep) {
Marker marker = googleMap.addMarker(new MarkerOptions()
.title(title)
.snippet(snippet)
.position(thePoint)
.icon(BitmapDescriptorFactory
      .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
markerId = marker.getId();

显示初始图像、标题和摘要时:

@Override
  public View getInfoContents(Marker marker)
    {
View v  = getLayoutInflater().inflate(R.layout.infowindow_layout, null);
  ImageView markerIcon = (ImageView) v.findViewById(R.id.marker_icon);
  TextView titleText = (TextView) v.findViewById(R.id.TitleId);
  titleText.setText(marker.getTitle());
  TextView snippetText = (TextView) v.findViewById(R.id.SnippetId);
  snippetText.setText(marker.getSnippet());
  Bitmap bitmap = myMarkersHash.get(marker.getId());
  markerIcon.setImageBitmap(bitmap);
  return v;

所以我知道我需要以某种方式(在 drawMarker 方法中)实现 filepid 以便我可以显示该标记的图像。但我不确定该怎么做。我不想用图标替换图像,而是我想要图像视图中带有图像的默认标记。

我已经为此苦苦挣扎了几个星期。所以现在我希望有人能够提供帮助。

编辑:

抱歉误解了你的问题。

您可以通过 HashMap 保存特定标记的文件路径,如下所示:

private HashMap<Marker, String> mMarkerImages = new HashMap<>();

private void drawMarker(LatLng point, String title, String snippet, String id, String filep) {
    Marker marker = googleMap.addMarker(new MarkerOptions()
            .title(title)
            .snippet(snippet)
            .position(thePoint)
            .icon(BitmapDescriptorFactory
                    .defaultMarker(BitmapDescriptorFactory.HUE_BLUE)));
    mMarkerImages.put(marker, filep);
}

稍后您可以使用相同的 HashMap 来获取特定于某些标记的文件路径:

@Override
public View getInfoContents(Marker marker) {
    String filep = mMarkerImages.get(marker);
    if (filep != null) {
        // File path was set for this marker, display your image
    }
    return null;
}