android:获得poi图片的一种方法(百度地图api)

本文介绍了在使用百度地图API时,如何通过 poi 的uid获取图片。由于百度地图API仅提供poi的URL,开发者需要自行解析和构建URL,例如使用""http://map.baidu.com/detail?qt=ninf&uid=...&detail=..."

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


百度地图api中关于poi检索的内容实际上是比较少的。


如果想做poi的介绍界面,需要 poi的图片。。。但是百度地图api中只提供了poi的url。没办法,用正则表达式自己搞!

一开始遇到的问题是无法解析getDetailUrl()的源码,后来发现这个url是个废弃的发火有么有搞错

自己构建如下"http://map.baidu.com/detail?qt=ninf&uid="+uid.get(i)+"&detail="+type.get(i)"

对于cater类型的网页,获取图片的正则表达式为

"(?<=<img src=\").*(?=\" class=\"head-img\"/>)"


关键源码如下:


public class webContent {
	public  byte[] readStream(InputStream inputStream) throws Exception {
		byte[] buffer = new byte[1024];
		int len = -1;
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

		while ((len = inputStream.read(buffer)) != -1) {
		byteArrayOutputStream.write(buffer, 0, len);
		}

		inputStream.close();
		byteArrayOutputStream.close();
		return byteArrayOutputStream.toByteArray();
		}

		public  String testGetHtml(String urlpath) throws Exception {
		URL url = new URL(urlpath);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		conn.setConnectTimeout(6 * 1000);
	
//	 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
//        StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
		conn.setRequestMethod("GET");

		if (conn.getResponseCode() == 200) {
		InputStream inputStream = conn.getInputStream();
		byte[] data = readStream(inputStream);
		Log.e("字节数组长度", data.length+"个字节");
		String html = new String(data);
		return html;
		}
		return null;
		}
		public String getTitle(String s) {
	        String regex;
	        String title = "";
	        ArrayList<String> list = new ArrayList<String>();
	        regex = "(?<=<img src=\").*(?=\" class=\"head-img\"/>)";
	        Pattern pa = Pattern.compile(regex);
	        Matcher ma = pa.matcher(s);
	        while (ma.find()) {
	            list.add(ma.group());
	        }
	        for (int i = 0; i < list.size(); i++) {
	            title = title + list.get(i);
	        }
	        return (title);
	    }	public ImageLoader loadImage(String  url,Context context){
		RequestQueue requestQueue = Volley.newRequestQueue(context);
		final LruCache<String, Bitmap> lruCache = new LruCache<String, Bitmap>(50);
		ImageCache imageCache = new ImageCache() {
			
			@Override
<span style="white-space:pre">		</span>public void putBitmap(String key, Bitmap value) {
<span style="white-space:pre">		</span>lruCache.put(key, value);
<span style="white-space:pre">		</span>}

<span style="white-space:pre">		</span>@Override
<span style="white-space:pre">		</span>public Bitmap getBitmap(String key) {
<span style="white-space:pre">		</span>return lruCache.get(key);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>};
<span style="white-space:pre">		</span>ImageLoader imageLoader = new ImageLoader(requestQueue, imageCache);
<span style="white-space:pre">		</span>return imageLoader;

<span style="white-space:pre">		</span>}}
		
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值