0

更新 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):在 java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274) 09-11 11:17:21.698: E/AndroidRuntime(1204): 在 java.util.concurrent.FutureTask.setException(FutureTask.java: 125) 09-11 11:17:21.698: E/AndroidRuntime(1204): 在 java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308) 09-11 11:17:21.698: E/AndroidRuntime( 1204): 在 java.util.concurrent.FutureTask.run(FutureTask.java:138) 09-11 11:17:21.698: E/AndroidRuntime(1204): 在 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:绑定或列索引超出范围:句柄 0x2475a8 09-11 11:17:21.698: E/AndroidRuntime(1204): at 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):在 com.mimi.ngsbusproject.GalleryActivity.Gallery(GalleryActivity.java:166) 09-11 11:17:21.698: E/AndroidRuntime(1204): 在 com.mimi.ngsbusproject.GalleryActivity.access$0(GalleryActivity.java:159 ) 09-11 11:17:21.698: E/AndroidRuntime(1204): 在 com.mimi.ngsbusproject.GalleryActivity$LoadGallery.doInBackground(GalleryActivity.java:85) 09-11 11:17:21.698: E/AndroidRuntime(1204 ): 在 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 个

请帮忙。我该如何解决这个问题?

抱歉我的语言技能很差。在此先感谢 :)

4

1 回答 1

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);
    }
于 2013-09-10T04:36:06.540 回答