改造获取位图

时间:2014-10-01 10:58:56

标签: 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

我可能无法以这种方式获取Bitmap,因为我看到它是字符串类型而不是位图而我无法转换它。我是对的,也许可以做到吗?

1 个答案:

答案 0 :(得分:0)

Square's 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 service = 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()或者只需使用其他Image加载库,例如Koush ION

使用Koush ION将图像加载到ImageView / Bitmaps中

//这是&#34; long&#34;建立一个ImageView请求的方法......它允许你设置标题等。

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");