Android的:如何获得图像文件目录到view.setBackgroundDrawable() [英] Android: How to get image files from directory into 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");
}
});
如果您想要的图像文件路径可以说,在文件file [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屋!