Android的:如何获得图像文件目录到view.setBackgroundDrawable() [英] Android: How to get image files from directory into view.setBackgroundDrawable()

查看:118
本文介绍了Android的:如何获得图像文件目录到view.setBackgroundDrawable()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让一个应用程序,可以从目录拍摄图像的android手机,并在布局中显示出来。我似乎工作我的方式,努力实现向后的方式解决。我知道使用 view.setBackgroundDrawable(Drawable.createFromPath(字符串将pathName)); 来显示图像,但我不知道如何从目录中获取图像的路径

I'm trying to make an app that can take images from a directory on the android phone and display them in a layout. I seem to be working my way towards a solution in a backwards manner. I know to use view.setBackgroundDrawable(Drawable.createFromPath(String pathName)); to display the image, but I don't know how to get the image's path from the directory.

我有什么做一个模糊的概念,但会在这个问题上AP preciate澄清。我认为,下一步是使用:

I have a vague idea of what to do, but would appreciate clarification on this matter. I think the next step is to use:

String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    File file[] = Environment.getExternalStorageDirectory().listFiles();
}

但我怎么过滤文件,以便只有图像文件被存储在我的文件[] ?如巴纽或.jpg / JPEG格式文件?在这之后,我应该使用文件[]。getPath 文件[]。getAbsolutePath ?我把结果存储在的String []

But how do I filter the files so that only image files get stored in my file[]? Such as .png or .jpg/.jpeg files? And after that, should I use files[].getPath or files[].getAbsolutePath? I would store the result in a String[].

我所主要要求的是核查,上述code应该工作。而且,我怎么可能过滤只存储的图像文件,如巴纽 .JPG .JPEG

What I am mainly asking for is verification that the above code should work. And also, how I might filter to store only image files such as .png, .jpg and .jpeg.

感谢您的时间。

推荐答案

您想要做这样的事情过滤:

You want to do something like this for filtering:

File[] file = folder.listFiles(new FilenameFilter() {

                @Override
                public boolean accept(File dir, String filename) {

                    return filename.contains(".png");
                }
            });

如果您想要的图像文件路径可以说,在文件fil​​e [0] ,你可以这样做:

If you want the filepath of the image of lets say the file at file[0], you would do this:

文件[0] .getAbsolutePath();

这篇关于Android的:如何获得图像文件目录到view.setBackgroundDrawable()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆