我尝试使用 Retrofit 库以这种方式从服务器获取位图:
@GET("/products/{ean}/image")
Bitmap getBitmapByEan(@Path("ean") String ean);
但是我收到一个错误:
Caused by: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1
我可能无法以这种方式获取位图,因为据我所知它是字符串类型而不是位图,并且我无法转换它。我是对的吗?也许可以这样做吗?
最佳答案
Square 的 Retrofit 目前支持像这样的 GSON 和 XML 内容格式类型
GSON:
Gson gson = new GsonBuilder()
.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
.registerTypeAdapter(Date.class, new DateTypeAdapter())
.create();
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.github.com")
.setConverter(new GsonConverter(gson))
.build();
GitHubService服务=restAdapter.create(GitHubService.class);
XML:
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint("https://api.soundcloud.com")
.setConverter(new SimpleXMLConverter())
.build();
SoundCloudService service = restAdapter.create(SoundCloudService.class);
您正在处理文件 I/O 类型,您需要构建自己的 SimpleBitmapConverter() 或仅使用其他图像加载库,例如 Koush ION
使用 Koush ION 将图像加载到 ImageView/位图中:
//这是构建 ImageView 请求的“长”方法...它允许您设置 header 等。
Ion.with(context)
.load("http://example.com/image.png")
.withBitmap()
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.animateLoad(spinAnimation)
.animateIn(fadeInAnimation)
.intoImageView(imageView);
// but for brevity, use the ImageView specific builder...
Ion.with(imageView)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.animateLoad(spinAnimation)
.animateIn(fadeInAnimation)
.load("http://example.com/image.png");
关于java - 改造获取位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26139804/