首页 > 解决方案 > 从地方api android获取照片

问题描述

private fun getPhotoAndDetail(id: String?) :Bitmap{ var bitmap:Bitmap= BitmapFactory.decodeResource(this.resources,R.drawable.common_full_open_on_phone)
   val  placeRequest=FetchPlaceRequest.builder(id!!, listOf(Place.Field.ADDRESS,Place.Field.PHOTO_METADATAS)).build()
    placesClient.fetchPlace(placeRequest).addOnSuccessListener{ it ->
        val place=it!!.place
        val photoMeta= place.photoMetadatas!![0]
        val photoRequest=FetchPhotoRequest.builder(photoMeta).setMaxWidth(500) // Optional.
            .setMaxHeight(300).build()
        placesClient.fetchPhoto(photoRequest).addOnSuccessListener{
            bitmap=it.bitmap
            Log.e(TAG,it.toString())
        }.addOnFailureListener{
            if (it is ApiException) {
                var apiException = it as ApiException;
                var statusCode = apiException.statusCode;
                // Handle error with given status code.
                Log.e(TAG, "Place not found: " + it.message)
        }}.addOnFailureListener{
            Log.e(TAG, "Place not found: " + it.message)
        }
    }
    return bitmap
}

我有这个函数可以从 Places Api 返回位图。但它永远不会在获取时达到成功的侦听器。在它获取有关地点的所有数据并显示它之前。

标签: androidkotlinnullgoogle-places-apiphoto

解决方案


推荐阅读