- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 AsyncTask 中编写了以下程序来从互联网加载图像并在 ImageView 中显示。如果我提供任何直接图像链接,但不使用 API 链接,程序就可以正常工作。
我的意思是,例如to have the cover of Farmer Boy from OpenLibrary ,我需要在 html 或浏览器中提供以下源代码: http://covers.openlibrary.org/b/isbn/9780385533225-S.jpg
但是,如果我在浏览器中输入上面的链接,浏览器会重定向到下面的地址。 http://ia700804.us.archive.org/zipview.php?zip=/12/items/olcovers4/olcovers4-M.zip&file=49855-M.jpg
我的问题是,我的代码适用于最后一个,但不适用于第一个。
如何使用第一个链接获取图像(在我的 android 应用程序中)?
代码:
private class getImageOpenLibrary extends AsyncTask<String, Void, Bitmap>
{
protected Bitmap doInBackground(String... args) {
URL newurl = null;
try {
//newurl = new URL("http://covers.openlibrary.org/b/isbn/"+args[0]+"-M.jpg"); // THIS DOES NOT WORK, args[0] = 9780064400039
newurl = new URL("http://ia700804.us.archive.org/zipview.php?zip=/12/items/olcovers4/olcovers4-M.zip&file=49855-M.jpg"); //THIS WORKS
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap mIcon_val = null;
try {
mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return mIcon_val;
}
//@Override
protected void onPostExecute(Bitmap result1)
{
ImageView mImageView = (ImageView) findViewById(R.id.cover);
mImageView.setImageBitmap(result1);
}
}
最佳答案
您应该处理重定向。该网址重定向到另一个网址。您应该在重定向 URL 上打开第二个连接。为了能够获得重定向 URL,设置 setInstanceFollowRedirects在连接上设置为 false 并读取 header 字段中的 Location
。
URL url = new URL("http://covers.openlibrary.org/b/isbn/9780385533225-S.jpg");
HttpURLConnection firstConn = (HttpURLConnection) url.openConnection();
firstConn.setInstanceFollowRedirects(false);
URL redirectURL = new URL(firstConn.getHeaderField("Location"));
URLConnection redirectConn = redirectURL.openConnection();
Bitmap bitmap = BitmapFactory.decodeStream(redirectConn.getInputStream());
关于android - 从 OpenLibrary API 在 ImageView 中加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20399547/
我正在开发一个需要能够平均三个数字的 Facebook 应用程序。但是,它总是返回 0 作为答案。这是我的代码: $y = 100; $n = 250; $m = 300; $number = ($y
我只是无法弄清楚这一点,也找不到任何对我来说有意义的类似问题。我的问题:我从数据库中提取记录,并在我的网页上以每个面板 12 条的倍数显示它们。因此,我需要知道有多少个面板可以使用 JavaScrip
我是一名优秀的程序员,十分优秀!