如何查询 Android MediaStore Content Provider,避免出现孤立图像?

2023-12-20

我正在尝试提供一个应用程序内活动,该活动显示照片缩略图 设备的媒体存储,并允许用户选择一个。用户做出后 选择后,应用程序会读取原始的全尺寸图像并对其进行处理。

我正在使用以下代码创建一个Cursor覆盖外部的所有图像 贮存:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView( R.layout.image_select );

    mGridView = (GridView) findViewById( R.id.image_select_grid );

    // Query for all images on external storage
    String[] projection = { MediaStore.Images.Media._ID };
    String selection = "";
    String [] selectionArgs = null;
    mImageCursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                                 projection, selection, selectionArgs, null );

    // Initialize an adapter to display images in grid
    if ( mImageCursor != null ) {
        mImageCursor.moveToFirst();
        mAdapter = new LazyCursorAdapter(this, mImageCursor, R.drawable.image_select_default);
        mGridView.setAdapter( mAdapter );
    } else {
        Log.i(TAG, "System media store is empty.");
    }
}

以下代码用于加载缩略图(显示的是 Android 2.x 代码):

// ...
// Build URI to the main image from the cursor
int imageID = cursor.getInt( cursor.getColumnIndex(MediaStore.Images.Media._ID) );
Uri uri = Uri.withAppendedPath( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                                Integer.toString(imageID) );
loadThumbnailImage( uri.toString() );
// ...

protected Bitmap loadThumbnailImage( String url ) {
    // Get original image ID
    int originalImageId = Integer.parseInt(url.substring(url.lastIndexOf("/") + 1, url.length()));

    // Get (or create upon demand) the micro thumbnail for the original image.
    return MediaStore.Images.Thumbnails.getThumbnail(mContext.getContentResolver(),
                        originalImageId, MediaStore.Images.Thumbnails.MICRO_KIND, null);
}

以下代码用于在用户做出选择后从 URL 加载原始图像:

public Bitmap loadFullImage( Context context, Uri photoUri  ) {
    Cursor photoCursor = null;

    try {
        // Attempt to fetch asset filename for image
        String[] projection = { MediaStore.Images.Media.DATA };
        photoCursor = context.getContentResolver().query( photoUri, 
                                                    projection, null, null, null );

        if ( photoCursor != null && photoCursor.getCount() == 1 ) {
            photoCursor.moveToFirst();
            String photoFilePath = photoCursor.getString(
                photoCursor.getColumnIndex(MediaStore.Images.Media.DATA) );

            // Load image from path
            return BitmapFactory.decodeFile( photoFilePath, null );
        }
    } finally {
        if ( photoCursor != null ) {
            photoCursor.close();
        }
    }

    return null;
}

我在某些 Android 设备(包括我自己的个人手机)上看到的问题是 我从查询中得到的光标onCreate()包含一些缺少实际全尺寸图像文件(JPG 或 PNG)的条目。 (就我的手机而言,图像已导入并随后被 iPhoto 删除)。

孤立条目可能有也可能没有缩略图,具体取决于擅离职守时缩略图是否在实际媒体文件之前生成。最终结果是应用程序显示实际不存在的图像的缩略图。

我有几个问题:

  1. 我可以向MediaStore将过滤掉的内容提供者 返回的图像中缺少媒体Cursor?
  2. 有没有办法或者API来强制MediaStore重新扫描并消除孤立条目?在我的手机上,我安装了 USB,然后卸载了外部介质,这应该会触发重新扫描。但孤儿条目仍然存在。
  3. 或者是我的方法存在根本性错误导致了这个问题?

Thanks.


好的,我发现了这个代码示例的问题。

In the onCreate()方法,我有这一行:

mImageCursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
                             projection, selection, selectionArgs, null );

这里的问题是它正在查询缩略图,而不是实际图像。 HTC 设备上的相机应用程序默认不会创建缩略图,因此此查询将无法返回尚未计算缩略图的图像。

相反,查询实际图像本身:

mImageCursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                             projection, selection, selectionArgs, null );

这将返回一个包含系统上所有全尺寸图像的光标。然后您可以致电:

Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
        imageId, MediaStore.Images.Thumbnails.MINI_KIND, null);

这将返回关联的全尺寸图像的中等尺寸缩略图,并在必要时生成它。要获得微型缩略图,只需使用MediaStore.Images.Thumbnails.MICRO_KIND反而。

这也解决了查找对原始全尺寸图像具有悬空引用的缩略图的问题。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何查询 Android MediaStore Content Provider,避免出现孤立图像? 的相关文章

随机推荐