java - 改造获取位图

标签 java android bitmap retrofit

我尝试使用 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/

相关文章:

仅包含选定字段的 JSON 的 Java 对象

java - LLVM 框架移植到 Java?

android - 设置 Spinner 适配器然后更改 EditText 输入类型

java - 日志: Queue's awaitRelease() has been interrupted abruptly while it wasn't released byte release() method

java - 带有本地化的 Android 文本输入验证

android - android 管理大量位图的有效方法

安卓 Intent ACTION_IMAGE_CAPTURE

android - view.getDrawingCache() 只工作一次

java - 使用 ant 或 maven 执行我的 groovy 脚本

java - 如何在java中引用外部文件的关键字