我有一个应用程序,其中标记是相机拍摄的图像并作为缩略图返回。现在我遇到的问题是,当实现onMarkerClick
以显示完整大小时,它只会返回在点击所有标记时拍摄的最新图像。
那么有没有办法将标记的ID或图像保存到该标记,以便当用户点击该标记以显示该标记的图像将显示的完整图像时?
这是我到目前为止的onMarkerClick代码:
@Override
public boolean onMarkerClick (Marker arg0){
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + destinationFile);
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
return false;
使用此代码,它会显示相机拍摄的最后一张图像。
有人可以帮忙,也可以提供一些能帮我解决这个问题的代码吗?
由于
更新
onMapLongClick
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE);
onActivityResult
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE)
{
File imageStorageFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"My Folder");
if (!imageStorageFolder.exists())
{
imageStorageFolder.mkdirs();
Log.d(TAG , "Folder created at: "+imageStorageFolder.toString());
}
if (data != null)
{
String filename = "image";
String fileNameExtension = ".jpg";
Environment.getExternalStorageDirectory();
String imageStorageFolder1 = File.separator+"My Folder"+File.separator;
markerImagePathMap.put(markerId, imageStorageFolder1 + filename + fileNameExtension);
Log.d(TAG, "the destination for image file is: " + markerImagePathMap );
if (data.getExtras() != null)
{
bitmap = (Bitmap)data.getExtras().get("data");
try
{
FileOutputStream out = new FileOutputStream((File) markerImagePathMap);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
catch (Exception e)
{
Log.e(TAG, "ERROR:" + e.toString());
}
MarkerOptions markerOptions = new MarkerOptions()
.draggable(true)
.snippet("snippet")
.title("Title")
.position(point)
.icon(BitmapDescriptorFactory
.fromBitmap(bitmap));
googleMap.addMarker(markerOptions);
onMarkerClick
@Override
public boolean onMarkerClick (Marker marker){
markerId = marker.getId();
if(markerImagePathMap.get(marker.getId() == null) != null){
// take image for marker
}
else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + markerImagePathMap.get(markerId));
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
}
return false;
问题是marker.getId();
Type mismatch: cannot convert from String to int
下if(markerImagePathMap.**get**(marker.getId() == null)){
以及The method get(int) in the type SparseArray<String> is not applicable for the arguments (boolean)
下{{1}}
答案 0 :(得分:0)
我打算将此作为评论,但它很草率。如果不知道你是如何处理你的项目的,那么这些代码可能会被关闭,但这个想法仍然存在。
创建以下类变量
private Map<String, String> markerImagePathMap;
创建一个HashMap,将您的标记ID映射到目标文件
@Override
public void onCreate...{
super.onCreate...;
markerImagePathMap = new HashMap<String, String>();
}
修改onActivityResult()方法以存储标记及其关联的文件路径
String destinationFilePath = imageStorageFolder1 + filename + fileNameExtension;
Marker marker = googleMaps.addMarker(markerOptions);
markerImagePathMap.put(marker.getId(), destinationFilePath);
在onMarkerCLick(标记标记)中,将方法更新为
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://" + markerImagePathMap.get(marker.getId());
intent.setDataAndType(imgUri, "image/*");
startActivity(intent);
return false;
编辑说明
将markerImagePathMap从SparseArray更改为HashMap
使用动态创建的标记的id作为Map的关键。
<强>更新强>
我觉得你的问题出现在onActivityResult()上。它看起来像
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == CAPTURE_IMAGE_THUMBNAIL_ACTIVITY_REQUEST_CODE){
File imageStorageFolder = new File(Environment.getExternalStorageDirectory()+File.separator+"My Folder");
if (!imageStorageFolder.exists()){
imageStorageFolder.mkdirs();
Log.d(TAG , "Folder created at: "+imageStorageFolder.toString());
}
if (data != null){
String filename = "image";
String fileNameExtension = ".jpg";
Environment.getExternalStorageDirectory();
String imageStorageFolder1 = File.separator+"My Folder"+File.separator;
/** CHANGE **/
String destinationFilePath = imageStorageFolder1 + filename + fileNameExtension;
Log.d(TAG, "the destination for image file is: " + destinationFilePath );
/** DONE CHANGE **/
if (data.getExtras() != null){
bitmap = (Bitmap)data.getExtras().get("data");
try{
FileOutputStream out = new FileOutputStream((File) markerImagePathMap);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
}catch (Exception e){
Log.e(TAG, "ERROR:" + e.toString());
}
MarkerOptions markerOptions = new MarkerOptions()
.draggable(true)
.snippet("snippet")
.title("Title")
.position(point)
.icon(BitmapDescriptorFactory
.fromBitmap(bitmap));
/** CHANGE **/
Marker marker = googleMap.addMarker(markerOptions);
markerImagePathMap.put(marker.getId(), destinationFilePath);
/** DONE CHANGE **/
}
}
}