获取SD卡上位图的ID

发布于 2025-01-05 01:05:22 字数 320 浏览 4 评论 0原文

我的应用程序有一个部分,其中一些图像显示在网格视图中。

在设置 gridview 之前,请完成以下操作:

  • 从 Internet 下载图像文件名列表
  • 将此列表与 Drawable 文件夹中已存在的项目进行比较
  • 如果图像不存在于 Drawable 文件夹中,则下载该图像并将其保存到SD 卡。

设置 gridview 时,我通过使用可绘制 ID 来实现,这些 ID 很容易获取,因为它们位于资源文件夹(“可绘制”)中。

但是如果我需要在 gridview 中包含 SD 卡图像,我该如何检索它们的 ID?

My app has a section where some images are displayed in a gridview.

Before the gridview is set up, the following is done:

  • Download list of image file names from the internet
  • Compare this list to items already present in the Drawable folder
  • If the image is not present in the Drawable folder, download it and save it to the SD card.

When setting up the gridview I do it by using the drawable IDs, which are easy to get as they are in a resource folder ("drawable").

But if I need to include the SD card images as well in the gridview, how can I retrive their IDs?

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

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

发布评论

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

评论(2

苏佲洛 2025-01-12 01:05:22

除了与应用程序一起打包的文件(例如下载到内部或外部存储)之外的任何位置的文件都没有资源 ID。

资源 ID 是引用打包资源(例如字符串、图像等)的简写方式。这些 id 是在构建时创建的,仅对内部资源有意义。

为了使用从外部源获取的图像,您可以通过文件名访问它们,我想,您将创建 ImageViews 并将其添加到您的 GridView 中。

如果您需要区分网格的不同图像(可能是出于触摸/单击目的),那么您需要处理的是 ImageView,而不是关心它们所包含的图像的资源 ID 或文件名。

Files anywhere other than those packaged with your app (downloaded to the internal or external storage, for instance) don't have resource ids.

Resource ids are a shorthand way of referencing the packaged resources such as strings, images, whatever. These ids are created at build-time and only have meaning for internal resources.

In order to use images obtained from an external source, you access them by filename and, I presume, you'll then create ImageViews which are added to your GridView.

If you need to discern between the different images of the grid (perhaps for touch/click purposes) then it's the ImageView that you deal with rather than being concerned with resource ids or filenames of the images they contain.

友谊不毕业 2025-01-12 01:05:22

我有一个代码从 sdcard 获取图像并将它们转换为 URI 数组,id 就像 URI,使用它们。

    private void getData() {
    File f = new File("/mnt/sdcard/");  
    File[] imagelist = f.listFiles(new FilenameFilter(){  

    public boolean accept(File dir, String name)  
    {  
        return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
    }  
    });  
    mFiles = new String[imagelist.length];  

    for(int i= 0 ; i< imagelist.length; i++)  
    {  
        mFiles[i] = imagelist[i].getAbsolutePath();  
    }  
    mUrls = new Uri[mFiles.length];  

    for(int i=0; i < mFiles.length; i++)  
    {  
        mUrls[i] = Uri.parse(mFiles[i]);             
    }
}

您可以像这样使用 uri:-

    ImageView imageView=new ImageView(this);
    imageView.setImageURI(mUrls[i]);

I was having a code to get images from sdcard and convert them into an array of URIs, the ids are like URIs, use them.

    private void getData() {
    File f = new File("/mnt/sdcard/");  
    File[] imagelist = f.listFiles(new FilenameFilter(){  

    public boolean accept(File dir, String name)  
    {  
        return ((name.endsWith(".jpg"))||(name.endsWith(".png")));
    }  
    });  
    mFiles = new String[imagelist.length];  

    for(int i= 0 ; i< imagelist.length; i++)  
    {  
        mFiles[i] = imagelist[i].getAbsolutePath();  
    }  
    mUrls = new Uri[mFiles.length];  

    for(int i=0; i < mFiles.length; i++)  
    {  
        mUrls[i] = Uri.parse(mFiles[i]);             
    }
}

You can use the uri like this:-

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