我想从手机上的所有图片中获取路径。我使用了这段代码,但它给了我dcim / camera /目录中的每个文件(它包括我不想得到的视频(.mp4)文件)。我想在每个目录中获得图像路径。
String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera/";
File targetDirector = new File(targetPath);
final File[] files = targetDirector.listFiles();
for (File file : files){
// file is path to file
}
答案 0 :(得分:1)
出于此目的,您必须查询Media Store Content Provider。在我的项目中,我使用以下方法获取列表,其中包含与每个图像相关的其他数据。
public static ArrayList<MediaStorePhoto> getAllPhotosFromExternalStorage(ContentResolver mContentResolver) {
MediaStorePhoto photo;
ArrayList<MediaStorePhoto> photoList = new ArrayList<>();
// which image properties are we querying
String[] projection = new String[]{
MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA
};
// content:// style URI for the "primary" external storage volume
Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// Make the query.
Cursor cur = mContentResolver.query(images,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
null // Ordering
);
Log.i("ListingImages", " query count=" + cur.getCount());
if (cur.moveToFirst()) {
String bucketId;
long id;
long size;
String bucket;
String date;
String name;
String dataUri;
int bucketIdColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_ID);
int idColumn = cur.getColumnIndex(
MediaStore.Images.Media._ID);
int bucketColumn = cur.getColumnIndex(
MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
int dateColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATE_TAKEN);
int sizeColumn = cur.getColumnIndex(
MediaStore.Images.Media.SIZE);
int nameColumn = cur.getColumnIndex(
MediaStore.Images.Media.DISPLAY_NAME);
int dataColumn = cur.getColumnIndex(
MediaStore.Images.Media.DATA);
do {
// Get the field values
bucketId = cur.getString(bucketIdColumn);
bucket = cur.getString(bucketColumn);
date = cur.getString(dateColumn);
size = cur.getLong(sizeColumn);
id = cur.getLong(idColumn);
name = cur.getString(nameColumn);
dataUri = cur.getString(dataColumn);
// Store photo in Photo object
photo = new MediaStorePhoto(String.valueOf(id), name,
bucket, date, String.valueOf(size), "null", dataUri, bucketId);
// Add photo to photo list
photoList.add(photo);
} while (cur.moveToNext());
}
cur.close();
return photoList;
}
注意:这里MediaStorePhoto
是一个类似
public class MediaStorePhoto implements Parcelable {
public static final Creator CREATOR = new Creator() {
public MediaStorePhoto createFromParcel(Parcel in) {
return new MediaStorePhoto(in);
}
public MediaStorePhoto[] newArray(int size) {
return new MediaStorePhoto[size];
}
};
private String id;
private String displayName;
private String bucket;
private String date;
private String size;
private String status;
private String dataUri;
private String bucketId;
public MediaStorePhoto(String id, String displayName, String bucket, String date, String size, String status, String dataUri, String bucketId) {
this.id = id;
this.displayName = displayName;
...
...
this.bucketId = bucketId;
}
public MediaStorePhoto(Parcel in) {
String[] data = new String[8];
in.readStringArray(data);
this.id = data[0];
this.displayName = data[1];
...
...
this.bucketId = data[7];
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringArray(new String[]{this.id,
this.displayName,
this.bucket,
...
...
this.bucketId});
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
...
...
...
public String getBucketId() {
return bucketId;
}
public void setBucketId(String bucketId) {
this.bucketId = bucketId;
}
}
因此,图像的uri将位于mediaStorePhoto.getDataUri()
同时mediaStorePhoto.getBucket()
将为您提供目录的名称。
答案 1 :(得分:-1)
好的,你所拥有的是一个良好的开端,但你需要稍微改变一下。首先,为了通过可变数量的文件路径,您需要一个在目录上调用自身的递归函数。
public void walk(File currDir, List<String> fileNames) {
File[] list = null;
try {
list = currDir.listFiles();
} catch (SecurityException e) {
// Can't read this file, just do nothing.
return;
}
File[] list = currDir.listFiles();
for (File f : list) {
if (f.isDirectory()) {
walk(f, fileNames);
} else {
// Add only images with the extension "jpg" (substitute your own logic here if necessary)
String fileName = f.getName();
String extension = fileName.substring(fileName.lastIndexOf(".")+1);
if ("jpg".equalsIgnoreCase(extension)) {
fileNames.add(fileName);
}
}
}
}
完成此操作后,您只需在当前目录中调用它即可启动链。但请注意,这可能需要一段时间,因此如果您需要大量图像,则需要在UI的单独线程中运行此功能。
List<String> files = new LinkedList<String>();
String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera/"; // TODO Change me as needed!
File targetDirectory = new File(targetPath);
walk(targetDirectory, files);
在此之后,files
应该在给定目录和所有子目录中包含所有JPG文件。