如何加载存储在特定文件夹(或ID)中的图片的缩略图?

时间:2014-09-14 21:38:34

标签: android mediastore

我有一个使用GridView的活动,我想在其中显示使用标准相机活动拍摄的图像的缩略图并存储在特定文件夹中。这个活动实现了LoaderManager.LoaderCallbacks接口,这里是onCreateLoader()方法的代码

...
...

private static final String APP_PICTURES_DIRECTORY_NAME = "Pictures to process";
private String[] mProjection = {
        MediaStore.Images.Thumbnails._ID,
        MediaStore.Images.Thumbnails.DATA,
        MediaStore.Images.Thumbnails.IMAGE_ID
};

private SimpleCursorAdapter mCursorAdapter;

...
...
...

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    String[] imagesProjection = {"_id", "_data"};
    Cursor imagesCursor;

    imagesCursor = getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            imagesProjection,
            MediaStore.Images.Media.DATA + " LIKE '%" + APP_PICTURES_DIRECTORY_NAME + "%'",
            null,
            null
    );

    String[] imageIds = new String[imagesCursor.getCount()];
    int i = 0;
    while (imagesCursor.moveToNext()) {
        imageIds[i++] = imagesCursor.getString(imagesCursor.getColumnIndex(MediaStore.Images.Media._ID));
    }

    String[] selectionArgs = {TextUtils.join(",", imageIds)};
    return new CursorLoader(
        JourneyAlbumActivity.this,
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
        mProjection,
        MediaStore.Images.Thumbnails.KIND + " = " + MediaStore.Images.Thumbnails.MINI_KIND +
                " AND " + MediaStore.Images.Thumbnails.IMAGE_ID + " IN (?)",
        selectionArgs,
        MediaStore.Images.Thumbnails.IMAGE_ID + " DESC"
    );
}

但结果中结果为空。如果我删除这个条款

... + " AND " + MediaStore.Images.Thumbnails.IMAGE_ID + " IN (?)"

一切都还可以但是在结果中我有一个用相机拍摄的所有图像的缩略图网格,而我只需要来自我特定目录的图像。如何为特定文件夹或特定ID的图像选择缩略图?

2 个答案:

答案 0 :(得分:2)

实际上,要使其工作,您必须直接在子句中插入参数:

return new CursorLoader(
    JourneyAlbumActivity.this,
    MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
    mProjection,
    MediaStore.Images.Thumbnails.KIND + " = " + MediaStore.Images.Thumbnails.MINI_KIND +
            " AND " + MediaStore.Images.Thumbnails.IMAGE_ID + " IN ("+TextUtils.join(",", ids)+")",
    selectionArgs,
    MediaStore.Images.Thumbnails.IMAGE_ID + " DESC"
);

致以最诚挚的问候,

答案 1 :(得分:0)

也许你可以尝试这样的事情:

System.out.println("getFilePaths()");
        File fileDir = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator +"folder_name"+ File.separator + AppConstant.PHOTO_ALBUM);
        if(!fileDir.exists()){
            System.out.println("fileDir does not exist");
            try{
                fileDir.mkdir();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        else
            System.out.println("fileDir DOES exist");
        File directory = fileDir;

        // check for directory
        if (directory.isDirectory()) {
            // getting list of file paths
            File[] listFiles = directory.listFiles();

            // Check for count
            if (listFiles.length > 0) {

                // loop through all files
                for (int i = 0; i < listFiles.length; i++) {

                    // get file path
                    String filePath = listFiles[i].getAbsolutePath();

                    // check for supported file extension
                    if (IsSupportedFile(filePath)) {
                        // Add image path to array list
                        filePaths.add(filePath);
                    }
                }
            } else {
                // image directory is empty
                Toast.makeText(
                        _context,
                        AppConstant.PHOTO_ALBUM
                                + " is empty. Please load some images in it !",
                        Toast.LENGTH_LONG).show();
            }

        } else {
            AlertDialog.Builder alert = new AlertDialog.Builder(_context);
            alert.setTitle("Error!");
            alert.setMessage(AppConstant.PHOTO_ALBUM
                    + " directory path is not valid! Please set the image directory name AppConstant.java class");
            alert.setPositiveButton("OK", null);
            alert.show();
        }