Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/227.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android BitmapDescriptorFactory.fromPath图像未显示在地图上_Android_Mobile - Fatal编程技术网

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

Android BitmapDescriptorFactory.fromPath图像未显示在地图上,android,mobile,Android,Mobile,我正试图把我的标记放在谷歌地图上。但尽管我调试(一切看起来都很好),我的标记并没有显示在地图上。你能帮我解决什么问题吗 String imageInSD = Config.APP_IMAGES_URL + sh.cover_image_file;[enter image description here][1]

我正试图把我的标记放在谷歌地图上。但尽管我调试(一切看起来都很好),我的标记并没有显示在地图上。你能帮我解决什么问题吗

                                             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。jpeg不是有效的文件系统路径。它看起来像一个缺少其方案的HTTP URL

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


如果您的
imageInSD
值实际上是URL,则需要先下载这些图像
BitmapDescriptorFactory
不会为您下载这些。而且大多数图像加载库都是针对
ImageView
这样的对象,所以大多数图像加载库对您没有帮助。你可以看看是否有人有办法使用像毕加索这样的库来填充标记。否则,在添加标记之前,请使用
HttpURLConnection
、OkHttp等下载图像。

下面是一个类的示例,可用于从网站下载并转换为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
类下载WebPageAsynch扩展异步任务
{
私有字符串文件名;
私有最终字符串标记=“DownloadWebpageAsynch”;
//onPostExecute显示异步任务的结果。
@凌驾
受保护的void onPostExecute(整数结果)
{
//在UI线程上执行此处的任何工作
//在您的情况下,为谷歌地图创建位图
BitmapDescriptor bd=BitmapDescriptorFactory.fromPath(文件名);
//使用google地图调用中的bd作为位图添加到地图
}
受保护的void下载完成(BufferedInputStream流入)
{
//在后台线程上完成下载后在此执行工作
//inStream是web服务器返回的内容
//安全文件到临时存储
尝试
{
File tempFile=File.createTempFile(“1”,“.jpg”);
tempFile.deleteOnExit();
fileName=tempFile.toString();
FileOutputStream fos=新的FileOutputStream(tempFile);
字节[]readyBytes=新字节[1000];
int numRead=inStream.read(readyBytes,0999);
while(numRead!=-1)
{
fos.write(就绪字节,0,numRead);
numRead=inStream.read(readyBytes,0999);
}
fos.close();
}捕获(IOE异常)
{
e、 printStackTrace();
}
}
@凌驾
受保护的整数doInBackground(字符串…URL)
{
//params来自execute()调用:params[0]是url。
尝试
{
下载URL(URL[0]);
}
捕获(IOE异常)
{
Log.d(标记“异常失败”+e.toString());
}
返回0;
}
//给定URL,建立HttpUrlConnection并检索
//网页内容以InputStream的形式显示,并返回为
//一串。
私有void downloadUrl(字符串myurl)引发IOException
{
BufferedInputStream为空;
downloadFailed=false;
尝试
{
URL=新URL(myurl);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setReadTimeout(10000);//10秒超时
conn.setConnectTimeout(10000);//10秒超时
conn.setDoInput(真);
//启动查询
连接();
responseCode=conn.getResponseCode();
如果(响应代码!=200)
{
Log.d(标记“下载失败”);
}
其他的
{
下载完成(新的BufferedInputStream(conn.getInputStream());
}
连接断开();
}
捕获(SocketTimeoutException e)
{
Log.d(标记“异常失败”+e.toString());
}
最后
{
如果(is!=null)
{
is.close();
}
}
}//私有字符串下载URL(字符串myurl)引发IOException

希望这能有所帮助。

imageInSD的确切位置在哪里?您的评论不是完整的文件系统路径。对不起,我找不到它。您的意思是什么?它是映像路径。我有一个“for”loop.Everytime marker必须获取另一个图像。调试时,我控制imageInSD Everytime get TRUE path。但在应用程序中,它不起作用。显示空映射。对于exm 1.loop-->imageInSD=www.abc.com/1.jpeg 2.loop-->imageInSD=www.abc.com/2.jpeg…此处为BitmapDescriptorFactory.fromPath(imageInSD)不工作,我想实际上我可以判断错误。是的,我需要从我的服务器获取每个图像的http url。但是在.icon(BitmapDescriptorFactory.fromPath(imageInSD))中,我不能。你能帮助我解决更多问题吗。Thanks@user3404273:“我需要从服务器获取每个图像的http url”--您需要自己下载这些图像。查看更新后的答案。谢谢,但是您有代码示例吗?我不太专业。根据您的话,我需要一些帮助来获取数据。@user3404273:我没有任何将
BitmapDescriptorFactory
用于Internet图像的示例,抱歉。我尝试了URL=new URL(Config.APP_IMAGES_URL+sh.cover_image_文件);位图bmp=BitmapFactory.decodeStream(URL.openConnection().getInputStream());--.icon(BitmapDescriptorFactory.fromBitmap(bmp))