android,如何从内存中的位图/可绘制对象获取资源标识符?
我正在使用一个数组列表,该数组列表是通过下载从 RSS 提要引用的文件来填充的。我认为目前它还没有保存到磁盘上。
- 如果我不保存到磁盘,实际上我是否会有资源标识符?
- 如果是这样,我该如何找回它?
强制性示例代码:
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;
}
提前致谢!
I am working with an arraylist populated by downloading files referenced from an RSS feed. I don't think at this point it's ever saved to disk.
- If I don't save to disk, would I, in fact, even have a resource identifier?
- If so, how would I retrieve it?
obligatory sample code:
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;
}
thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
恐怕答案是否定的。
您只能获取 /res 文件夹中声明的文件的资源标识符。如果您将位图下载到您的应用程序并想要存储它们,我目前能想到的唯一解决方案是将它们保存为文件(缓存或外部)或数据库中。
I'm afraid the answer is no.
You only get a resource identifier for files that are declared in the /res folder. If you download Bitmaps to your application and want to store them, the only solutions I'm currently able to think right now are saving them as files (cache or external) or in a database.