我想从SD卡获取具有多个图像的文件夹。
我已经完成了获取所有文件夹,但我想获得有图像的文件夹。
这是我的activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.domore.folderlist.MainActivity">
<!--<ListView-->
<!--android:id="@+id/listview"-->
<!--android:layout_width="fill_parent"-->
<!--android:layout_height="wrap_content">-->
<!--</ListView>-->
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:numColumns="2"
android:stretchMode="columnWidth" >
</GridView>
</LinearLayout>
这是我的MainActivity.java
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
private File file;
private List<String> myList;
private ArrayAdapter adapter;
GridView listview;
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
File temp_file=new File(file,myList.get(position));
if(!temp_file.isFile()) {
file = new File(file, myList.get(position));
Log.e("CLICK PATH",""+file);
File list[] = file.listFiles();
myList.clear();
for (int i = 0; i < list.length; i++) {
myList.add(list[i].getName());
}
//adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, myList);
listview.setAdapter(adapter);
// listview.setAdapter(new ImageAdapter(this,myList));
}
}
@Override
public void onBackPressed() {
// super.onBackPressed();
String parent = file.getParent().toString();
Log.e("PATH",parent);
file = new File(parent) ;
File list[] = file.listFiles();
myList.clear();
for( int i=0; i< list.length; i++)
{
myList.add( list[i].getName() );
}
// listview.setAdapter(new ImageAdapter(this,myList));
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,myList);
listview.setAdapter(adapter);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview=(GridView)findViewById(R.id.gridview);
myList=new ArrayList<String>();
String root_sd= Environment.getExternalStorageDirectory().getPath().toString();
Log.e("PATH",""+root_sd);
file=new File(root_sd);
File list[]=file.listFiles();
for(int i=0;i<list.length;i++){
myList.add(list[i].getName());
}
adapter=new ArrayAdapter(this,android.R.layout.simple_list_item_1,myList);
listview.setAdapter(adapter);
// listview.setAdapter(new ImageAdapter(this,myList));
listview.setOnItemClickListener(this);
}
}
请帮我解决
答案 0 :(得分:0)
在我的应用程序中,我编写此代码以加载所有照片:
Resolver = getContentResolver();
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {MediaStore.Images.ImageColumns.DATA};
Cursor c = null;
SortedSet<String> dirList = new TreeSet<String>();
String[] directories = null;
if (u != null) {
c = managedQuery(u, projection, null, null, null);
}
if ((c != null) && (c.moveToFirst())) {
do {
String tempDir = c.getString(0);
tempDir = tempDir.substring(0, tempDir.lastIndexOf("/"));
try {
dirList.add(tempDir);
} catch (Exception e) {
e.printStackTrace();
}
}
while (c.moveToNext());
directories = new String[dirList.size()];
dirList.toArray(directories);
}
for (int i = 0; i < dirList.size(); i++) {
File imageDir = new File(directories[i]);
File[] imageList = imageDir.listFiles();
if (imageList == null)
continue;
for (File imagePath : imageList) {
try {
if (imagePath.isDirectory()) {
imageList = imagePath.listFiles();
}
if (imagePath.getName().toLowerCase(Locale.US).endsWith(".jpg")
|| imagePath.getName().toLowerCase(Locale.US).endsWith(".jpeg")
|| imagePath.getName().toLowerCase(Locale.US).endsWith(".png")) {
String path = imagePath.getAbsolutePath();
PhotosList.add(path);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 1 :(得分:0)
试试这个
public void searchImageFromSpecificDirectory() {
String path = null;
String uri = MediaStore.Images.Media.DATA;
// if GetImageFromThisDirectory is the name of the directory from which image will be retrieved
String condition = uri + " like '%/GetImageFromThisDirectory/%'";
String[] projection = { uri, MediaStore.Images.Media.DATE_ADDED,
MediaStore.Images.Media.SIZE };
Vector additionalFiles = null;
try {
if (additionalFiles == null) {
additionalFiles = new Vector<String>();
}
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection,
condition, null, null);
if (cursor != null) {
boolean isDataPresent = cursor.moveToFirst();
if (isDataPresent) {
do {
path = cursor.getString(cursor.getColumnIndex(uri));
System.out.println("...path..."+path);
additionalFiles.add(path);
}while(cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
答案 2 :(得分:0)
尝试以下代码行: -
File[] file = Environment.getExternalStorageDirectory().listFiles();
for (File f : file)
{
if (f.isDirectory()) {
file[] innerFiles = f.listFiles();
for(int i=0; i< innerFiles.length;i++){
Log.i("Name", innerFiles[i].getPath() + "");
///here you make the check that FOLDER consits IMAGE ofr NOT
if( innerFiles[i].getPath().contains(".JPG") || innerFiles[i].getPath().contains(".jpg"))
{
// YOU CAN PERFORM YOURS OPERATION HERE
}
}
}
if (f.isFile()) { ... do stuff }
}
if (f.isFile()) { ... do stuff }
}