1

我有一些图片存储在外部 SD 卡中,我想在GridView. 我知道媒体扫描仪已经创建了缩略图,因为我可以使用标准图库应用程序浏览我的图片文件夹,但我不知道这些缩略图的位置,所以我不知道如何将它们添加到我的GridView.

我正在尝试通过以下方式获取缩略图:

MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong(_imageUri.getLastPathSegment()), type, null)

_imageUri必须是内容模式 Uri,所以我的问题是找到一种将图像的文件模式 Uris 转换为内容模式 Uri 的方法。不幸的是我不知道该怎么做。我已经看到很多推荐使用的 SO 线程,Uri.parse()但它只是不起作用,所以我正在寻找不同的解决方案。

我目前的方法是使用媒体扫描仪扫描单个文件并尝试UrionScanCompleted回调中检索内容。代码是:

public class SimpleMediaScanner implements MediaScannerConnectionClient {
    private MediaScannerConnection mMSC;
    private File mFile;
    private MyAdapter mAdapter;

    public SimpleMediaScanner(Context c, File f, MyAdapter a) {
        mAdapter = a;
        mFile = f;
        mMSC = new MediaScannerConnection(c, this);
        mMSC.connect();
    }

    @Override
    public void onMediaScannerConnected() {
        mMSC.scanFile(mFile.getAbsolutePath(), null);
    }

    @Override
    public void onScanCompleted(String path, Uri uri) {
        // Store the content scheme Uri of the scanned file
        // in a public field of the adapter
        mAdapter.mThumbUri = uri;
        mMSC.disconnect();
    }
}

我从我的扩展实例化这个类SimplecursorAdapter

SimpleMediaScanner sms = new SimpleMediaScanner(mContext, new File(filepath), this);

不幸的是,返回的mAdapter.mThumbUri值总是null. 有人可以告诉我我做错了什么吗?TIA

4

1 回答 1

1

您是否尝试过MediaStorage.Images.Thumbnails类?它提供了两种静态getThumbnail(...)方法,可以满足您的需求。

于 2012-08-26T08:33:48.087 回答