简体   繁体   English

android,如何从内存中获取资源标识符/ drawable在内存中?

[英]android, how to get resource identifier from bitmap/drawable in memory?

I am working with an arraylist populated by downloading files referenced from an RSS feed. 我正在使用通过下载从RSS源引用的文件填充的arraylist。 I don't think at this point it's ever saved to disk. 我不认为此时它已保存到磁盘。

  1. If I don't save to disk, would I, in fact, even have a resource identifier? 如果我不保存到磁盘,我实际上甚至会有资源标识符吗?
  2. 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! 提前致谢!

I'm afraid the answer is no. 我担心答案是否定的。

You only get a resource identifier for files that are declared in the /res folder. 您只获得在/ res文件夹中声明的文件的资源标识符。 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. 如果您将Bitmaps下载到您的应用程序并想要存储它们,我现在能够想到的唯一解决方案是将它们保存为文件(缓存或外部)或数据库中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM