[英]android, how to get resource identifier from bitmap/drawable in memory?
我正在使用通过下载从RSS源引用的文件填充的arraylist。 我不认为此时它已保存到磁盘。
强制性样本代码:
URL url = new URL("http://someurl.com/rss");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
NodeList postList = doc.getElementsByTagName("item");
thumbIds = new Integer[postList.getLength()];
for (int i = 0; i < postList.getLength(); i++) {
// extract post
Node post = postList.item(i);
Element postElement = (Element) post;
NodeList titleList = postElement.getElementsByTagName("media:thumbnail");
Element titleElement = (Element) titleList.item(0);
String myUrl = titleElement.getAttribute("url").toString();
if(myUrl != null) {
thumbs.add(new BitmapDrawable(fetchBitmap(myUrl)));
}
}
public static Bitmap fetchBitmap(String url) {
URL newurl = null;
try {
newurl = new URL(url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Bitmap returnBitmap = null;
try {
returnBitmap = BitmapFactory.decodeStream(newurl.openConnection()
.getInputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return returnBitmap;
}
提前致谢!
我担心答案是否定的。
您只获得在/ res文件夹中声明的文件的资源标识符。 如果您将Bitmaps下载到您的应用程序并想要存储它们,我现在能够想到的唯一解决方案是将它们保存为文件(缓存或外部)或数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.