MediaStore.Images.Thumbnails.getThumbnail返回错误的缩略图,而不是空 [英] MediaStore.Images.Thumbnails.getThumbnail returns wrong thumbnail instead of NULL

查看:572
本文介绍了MediaStore.Images.Thumbnails.getThumbnail返回错误的缩略图,而不是空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑以下情况,如在这个附加的图片。 三张照片,其中之一是一个大的GIF文件(3MP)。

Consider the scenario as in this attached picture. Three photos, one of them is a large GIF file (3MP).

我查询MediaStore,以获取相应的缩略图。如果我通过CursorLoader初始化光标与此排序顺序:

I'm querying MediaStore in order to retrieve the correspondent thumbnails. If I initialize the Cursor via CursorLoader with this sortOrder:

MediaStore.Images.Media.DATE_ADDED + " DESC"" (full code is attached)

会发生什么: MediaStore返回previous成功检索缩略图(请看到这张照片 )。

What happens: MediaStore returns the previous successfully retrieved thumbnail (please see this picture).

预期的行为:当MediaStore无法检索特定图像的缩略图由于某种原因,它必须返回NULL,根据其的Javadoc:...返回Bitmap实例它。可能是无效的话与origId相关联的原始图像不存在或存储器是不够的。

Expected behaviour: when MediaStore cannot retrieve the thumbnail of a given image for some reason it has to return NULL, as per its Javadoc: "... Returns A Bitmap instance. It could be null if the original image associated with origId doesn't exist or memory is not enough."

如果我初始化光标与此排序顺序:

If I initialize the Cursor with this sortOrder:

MediaStore.Images.Media.DATE_ADDED + " ASC""

它运行得很好(请参阅本图片)。不过,我不能简单地改变排序顺序,因为需求是最先显示最新的照片。

It runs just fine (please refer to this image). However I can't simply change the sortOrder since the requirement is to show the newest pictures first.

下面是我的示例code和这里是完整的示例项目还有的the三个图像用于再现。我失去了一些东西?有没有人有一个想法是什么可能是错的?

Below is my sample code and here is the complete sample project as well as the three images used to reproduce. Am I missing something? Does anyone have an idea what may be wrong?

package com.example.getimagefrommediastore;

import android.app.Activity;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v4.content.CursorLoader;
import android.widget.ImageView;
import android.widget.TextView;

public class GetThumbnailsFromMediaStoreSampleActivity extends Activity {

TextView mThumb_id_01;
TextView mThumb_id_02;
TextView mThumb_id_03;
ImageView mImg_01;
ImageView mImg_02;
ImageView mImg_03;
boolean isThumb01 = true; // Simple flag to control this example
boolean isThumb02 = true;
Cursor mCursorLoader;
int mColumnIndex;
long mOrigId; // Original image id associated with thumbnail of interest

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Just initializing views
    mThumb_id_01 = (TextView) findViewById(R.id.thumb_id_01);
    mThumb_id_02 = (TextView) findViewById(R.id.thumb_id_02);
    mThumb_id_03 = (TextView) findViewById(R.id.thumb_id_03);
    mImg_01 = (ImageView) findViewById(R.id.thumb_01);
    mImg_02 = (ImageView) findViewById(R.id.thumb_02);
    mImg_03 = (ImageView) findViewById(R.id.thumb_03);

    // Initializing CursorLoader
    mCursorLoader = initializeCursorLoader();
    mColumnIndex = mCursorLoader.getColumnIndex(MediaStore.Images.Media._ID);

    // Go thru all the images in the device (EXTERNAL_CONTENT_URI)
    // In this example there are only three images
    for (int i = 0; i < mCursorLoader.getCount(); i++) {
        mCursorLoader.moveToPosition(i);
        mOrigId = mCursorLoader.getInt(mColumnIndex);

        // Update views
        chooseViewToUpdate();
    }
}

private Cursor initializeCursorLoader() {
    String[] COLUMNS = {
            MediaStore.Images.Thumbnails._ID, MediaStore.Images.Media.DATA
    };

    CursorLoader cursorLoader = new CursorLoader(
    GetThumbnailsFromMediaStoreSampleActivity.this, // Context
    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, // Uri
    COLUMNS, // Projection
    null, // Selection
    null, // Selection Args

    // Sort Order: DESC = newest first
    // Sort Order: ASC = oldest first

    MediaStore.Images.Media.DATE_ADDED + " DESC");

    // *** NOTE ***
    // With:
    //
    // MediaStore.Images.Media.DATE_ADDED + " ASC"
    //
    // It runs just fine (MediaStore returns 'null' for invalid thumbnails)
    // The problem seems to reside on the " DESC" tag.
    //
    // How bizarre is that?

    return cursorLoader.loadInBackground();
}

private void chooseViewToUpdate() {
    if (isThumb01) {
        updateUI(mThumb_id_01, mImg_01);
        isThumb01 = false;
    } else if (isThumb02) {
        updateUI(mThumb_id_02, mImg_02);
        isThumb02 = false;
    } else {
        updateUI(mThumb_id_03, mImg_03);
    }
}

private void updateUI(TextView textView, ImageView imgView) {
    textView.setText("ID:" + String.valueOf(mOrigId));

    Bitmap mediaStoreThumbmail = MediaStore.Images.Thumbnails.getThumbnail(
            this.getContentResolver(),
            mOrigId,
            MediaStore.Images.Thumbnails.MICRO_KIND, null);

    if (mediaStoreThumbmail != null) {
        imgView.setImageBitmap(mediaStoreThumbmail);
    }
}

我充满对Android的一个bug反正。在此先感谢!

推荐答案

我米只是猜测这里。当你问的操作系统是创建一个新的图像,下一获取行降序光标再次产生相同的图像的MICRO_KIND

I"m just guessing here. When you ask for the MICRO_KIND the os is creating a new image which gets next in line on DESC cursor producing the same image again.

一个解决办法是加载一个ArrayList的图片ID的。然后从ArrayList中工作的缩略图后走了。

One work around is to load an ArrayList for the image id's. Then to go after the thumbnails working from the ArrayList.

可能性使用MINI_KIND和bmoptions.inSampleSize = 2试试你的code;

Possibility try your code using the MINI_KIND and bmoptions.inSampleSize = 2;

 final BitmapFactory.Options bmOptions = new BitmapFactory.Options();
            bmOptions.inSampleSize =2;



                    Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(
                            context.getContentResolver(), newImageId,
                            MediaStore.Images.Thumbnails.MINI_KIND,
                            bmOptions);

这篇关于MediaStore.Images.Thumbnails.getThumbnail返回错误的缩略图,而不是空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆