android,如何从内存中的位图/可绘制对象获取资源标识符?

发布于 2024-12-17 21:03:44 字数 1726 浏览 0 评论 0原文

我正在使用一个数组列表,该数组列表是通过下载从 RSS 提要引用的文件来填充的。我认为目前它还没有保存到磁盘上。

  1. 如果我不保存到磁盘,实际上我是否会有资源标识符?
  2. 如果是这样,我该如何找回它?

强制性示例代码:

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.

  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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一笑百媚生 2024-12-24 21:03:44

恐怕答案是否定的。

您只能获取 /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文