更新Logcat和Java代码。
您好我想从SD卡上的指定文件夹中获取所有图片ID,完整路径 这是我的代码。
private void Gallery() {
ImageList = new ArrayList<GalleryDetail>();
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
Cursor image_cursor;
image_cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
new String[] {currentDir.toString()}, null);
if (image_cursor != null) {
while (image_cursor.moveToNext()) {
GalleryDetail img = new GalleryDetail();
String image_id = image_cursor.getString(image_cursor
.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
String image_path = image_cursor.getString(image_cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
Log.e("get Path.", image_path);
img.setImageId(image_id);
img.setImagePath(image_path);
ImageList.add(img);
}
// List of file path
FilePathStrings = new String[ImageList.size()];
for (int i = 0; i < ImageList.size(); i++) {
FilePathStrings[i] = ImageList.get(i).getImagePath();
}
}
}
但它对我不起作用,log cat向我显示此消息。
09-11 11:17:21.698:E / AndroidRuntime(1204):致命异常:AsyncTask#1 09-11 11:17:21.698:E / AndroidRuntime(1204):java.lang.RuntimeException:执行doInBackground()时发生错误 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.os.AsyncTask $ 3.done(AsyncTask.java:200) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.util.concurrent.FutureTask $ Sync.innerSetException(FutureTask.java:274) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.util.concurrent.FutureTask.setException(FutureTask.java:125) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:308) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.util.concurrent.FutureTask.run(FutureTask.java:138) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:581) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.lang.Thread.run(Thread.java:1019) 09-11 11:17:21.698:E / AndroidRuntime(1204):引起:android.database.sqlite.SQLiteException:绑定或列索引超出范围:handle 0x2475a8 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:158) 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114) 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.content.ContentProviderProxy.bulkQueryInternal(ContentProviderNative.java:330) 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.content.ContentProviderProxy.query(ContentProviderNative.java:366) 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.content.ContentResolver.query(ContentResolver.java:262) 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.app.Activity.managedQuery(Activity.java:1644) 09-11 11:17:21.698:E / AndroidRuntime(1204):at com.mimi.ngsbusproject.GalleryActivity.Gallery(GalleryActivity.java:166) 09-11 11:17:21.698:E / AndroidRuntime(1204):at com.mimi.ngsbusproject.GalleryActivity.access $ 0(GalleryActivity.java:159) 09-11 11:17:21.698:E / AndroidRuntime(1204):at com.mimi.ngsbusproject.GalleryActivity $ LoadGallery.doInBackground(GalleryActivity.java:85) 09-11 11:17:21.698:E / AndroidRuntime(1204):at com.mimi.ngsbusproject.GalleryActivity $ LoadGallery.doInBackground(GalleryActivity.java:1) 09-11 11:17:21.698:E / AndroidRuntime(1204):在android.os.AsyncTask $ 2.call(AsyncTask.java:185) 09-11 11:17:21.698:E / AndroidRuntime(1204):at java.util.concurrent.FutureTask $ Sync.innerRun(FutureTask.java:306) 09-11 11:17:21.698:E / AndroidRuntime(1204):... 4更多
请帮忙。我该如何解决这个问题?
对不起我非常糟糕的语言技巧。谢谢你提前:)
答案 0 :(得分:1)
在mainfest.xml中添加了此权限吗?
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
更新
ArrayList<String> mFiles = new ArrayList<String>();
File mDirectory;
String folderPath = "/mnt/sdcard/download";
mDirectory = new File(folderPath);
// Get the files in the directory
File[] files = mDirectory.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
mFiles.add(f.getAbsolutePath());
}
}
更新
getAll图像的递归函数
private ArrayList<String> allImages = new ArrayList<String>();
String[] extensions = { "jpg", "png", "jpeg", "JPG", "PNG", "JPEG" };
private void loadAllImages(String rootFolder) {
File file = new File(rootFolder);
if (file.isDirectory()) {
File[] files = file.listFiles();
if (files != null && files.length > 0) {
for (File f : files) {
if (f.isDirectory()) {
loadAllImages(f.getAbsolutePath());
} else {
for (int i = 0; i < extensions.length; i++) {
if (f.getAbsolutePath().endsWith(extensions[i])) {
allImages.add(f.getAbsolutePath());
}
}
}
}
}
}
}
更新要获取图库图像
private String[] arrPath;
private boolean[] thumbnailsselection;
private int ids[];
private int count;
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, new String[] {"%myimagesfolder%"}, orderBy); //UPDATE HERE
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
count = imagecursor.getCount();
arrPath = new String[this.count];
ids = new int[count];
for (int i = 0; i < count; i++) {
imagecursor.moveToPosition(i);
ids[i] = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}