代码之家  ›  专栏  ›  技术社区  ›  mportuesisf

如何查询Android MediaStore内容提供商,避免孤立图像?

  •  37
  • mportuesisf  · 技术社区  · 16 年前

    我试图提供一个在应用程序中的活动,其中显示的缩略图中的照片 选择后,应用程序将读取原始的全尺寸图像并处理它。

    我使用以下代码创建 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.");
        }
    }
    

    // ...
    // 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删除)。

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

    1. 有什么问题我可以问你吗 MediaStore 将筛选出的内容提供商 返回的图像中缺少媒体 ?
    2. 媒体商店 重新扫描并删除孤立条目?在我的手机上,我安装了USB,然后卸下了外部媒体,这应该会触发重新扫描。但孤儿条目仍然存在。

    谢谢。

    2 回复  |  直到 16 年前
        1
  •  61
  •   mportuesisf    15 年前

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

    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 相反。

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

        2
  •  7
  •   Tamas    15 年前

    推荐文章