简体   繁体   中英

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. 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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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