BitmapDescriptorFactory.fromPath图像未在地图上显示

时间:2017-02-19 21:17:05

标签: android mobile

我想把我的标记放在谷歌地图上。但是虽然我调试(每个人看起来很好),我的标记没有显示在地图上。你能帮我解决一下这个问题吗?

                                             String imageInSD = Config.APP_IMAGES_URL + sh.cover_image_file;[enter image description here][1]



                                                                customMarker = googleMap.addMarker(new MarkerOptions()
                                                                        .position(markerLatLng)
                                                                        .title(sh.name)
                                                                        .snippet(sh.description.substring(0, Math.min(sh.description.length(), 80)) + "...")
                                                                        .icon(BitmapDescriptorFactory.fromPath(imageInSD)) // in debug looking www.asd.com/abc.jpg(right paths)
                                                                        .anchor(0.5f, 1));

我也试过这个

.icon(BitmapDescriptorFactory.FromFile(imageInSD))

但没有工作?哪里有问题 在调试中寻找正确的路径。添加截图。但是在应用程序映射中为null

2 个答案:

答案 0 :(得分:0)

www.abc.com/1.jpeg不是有效的文件系统路径。它看起来像是缺少其方案的HTTP URL。

fromPath()采用文件系统路径。给定File对象指向您的图片后,使用getAbsolutePath()将其转换为文件系统路径的String表示形式,并将该值传递给fromPath()

如果您的imageInSD值实际上是网址,则需要先下载这些图片。 BitmapDescriptorFactory不会为您下载这些内容。大多数图像加载库都针对ImageView之类的东西,所以大多数都不会帮助你。您可以看到是否有人使用像Picasso这样的库来填充标记。否则,在添加标记之前,请使用HttpURLConnection,OkHttp等下载图像。

答案 1 :(得分:0)

这是一个类的示例,可用于从网站下载并转换为gmaps API可以显示的内容。我从一个正在研究的项目中解除了这个问题,并提取了不适用的东西 - 没有尝试编译它,但它应该是关闭的。

class DownloadWebpageAsynch  extends AsyncTask<String, Void, Integer>
{
    private String fileName;
    private final String TAG = "DownloadWebpageAsynch";

    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(Integer result)
    {
        // Perforrm any work here on the UI thread
    // in your case, create the bitmap for the google map

    BitmapDescriptor bd = BitmapDescriptorFactory.fromPath(fileName);

    // use bd in google map calls as the bitmap to add to the map
    }

    protected void DownloadComplete(BufferedInputStream inStream)
    {
    // Perform work here upon download complete on the background thread
        // inStream is what was returned by the web server

    // Safe file to temp storage
    try
    {
        File tempFile = File.createTempFile("1", ".jpg");
        tempFile.deleteOnExit();
        fileName = tempFile.toString();
        FileOutputStream fos = new FileOutputStream(tempFile);
        byte[] readyBytes = new byte[1000];
        int numRead = inStream.read(readyBytes, 0, 999);
        while(numRead != -1)
        {
        fos.write(readyBytes, 0, numRead);
        numRead = inStream.read(readyBytes, 0, 999);
        }
        fos.close();
    } catch (IOException e)
    {
        e.printStackTrace();
    }
    }



    @Override
    protected Integer doInBackground(String... urls)
    {
        // params comes from the execute() call: params[0] is the url.
        try
        {
        downloadUrl(urls[0]);
        }
        catch (IOException e)
        {
            Log.d(TAG, "Failed with exception " + e.toString());
        }
        return 0;
    }

    // Given a URL, establishes an HttpUrlConnection and retrieves
    // the web page content as a InputStream, which it returns as
    // a string.
    private void downloadUrl(String myurl) throws IOException
    {
        BufferedInputStream is = null;
        downloadFailed = false;

        try
        {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000); // 10 second timeout
            conn.setConnectTimeout(10000); // 10 second timeout
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            responseCode = conn.getResponseCode();
            if(responseCode != 200)
            {
                Log.d(TAG, "Download Failed");
            }
            else
            {
          DownloadComplete(new BufferedInputStream(conn.getInputStream());
            }
            conn.disconnect();
        }
        catch (SocketTimeoutException e)
        {
      Log.d(TAG, "Failed with exception " + e.toString());
        }
        finally
        {
            if (is != null)
            {
                is.close();
            }
        }
    }// private String downloadUrl(String myurl) throws IOException

希望这有帮助。