从内容Uri图像解码后,Android Bitmap图像返回null

时间:2016-04-13 11:21:06

标签: android bitmap android-asynctask

我正在处理从SD卡加载所有图像并在GridView中显示。我使用AsyncTask从后台线程加载图像内容Uri并通过BitmapFactory.Options进行解码。但是,问题是解码后的位图图像返回 NULL 。有谁可以让我知道错误或帮助解决?谢谢!

在onCreate()方法

 LoadImageTask task = new LoadImageTask();
    task.execute();

使用游标

加载图像内容Uri的AsyncTask
public class LoadImageTask extends AsyncTask<String, Void, String> {

    private ProgressDialog showDialog;

    protected void onPreExecute() {
        super.onPreExecute();
        showDialog = new ProgressDialog(GalleryActivity.this);
        showDialog.setMessage("Loading images...");
        showDialog.setCancelable(false);
        showDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        showDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {

        try {
            String[] list = {MediaStore.Images.Media._ID};
            cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, list,
                    null, null, MediaStore.Images.Media._ID);
            columnIndex = cursor.getColumnIndex(MediaStore.Images.Media._ID);

            for(int i = 0; i < cursor.getCount(); i++) {
                cursor.moveToPosition(i);
                int imageID = cursor.getInt(columnIndex);
                uri = Uri.withAppendedPath(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imageID + "");
            }
        }catch(Exception e){
             Log.e(TAG,e + "");
            }
            return null;
        }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        showDialog.dismiss();
        adapter = new ImageAdapter(GalleryActivity.this, cursor);
        mGridView.setAdapter(adapter);
    }
}

ImageAdapter

public class ImageAdapter extends BaseAdapter {

Context mContext;
Cursor c;

public ImageAdapter(Context context, Cursor cursor) {
    mContext = context;
    c = cursor;
}

@Override
public int getCount() {
    return c.getCount();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public  View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;

    if (convertView == null){
        convertView = LayoutInflater.from(mContext).inflate(R.layout.grid_item,parent,false);;
        holder = new ViewHolder(convertView);
        convertView.setTag(holder);
    }else{
        holder = (ViewHolder)convertView.getTag();
    }

    Bitmap bm = GalleryActivity.decodeSampledBitmapFromUri(GalleryActivity.uri + "", 200, 200);
    Log.i(GalleryActivity.TAG, bm + "");  // after log return null
    holder.gridImageView.setImageBitmap(bm);

    return convertView;
}}

位图解码

public static Bitmap decodeSampledBitmapFromUri(String path, int reqWidth, int reqHeight) {

    Bitmap bm;
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    bm = BitmapFactory.decodeFile(path, options);

    return bm;
}

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight)
{
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 2;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
            inSampleSize = Math.round((float)height / (float)reqHeight);
        } else {
            inSampleSize = Math.round((float)width / (float)reqWidth);
        }
    }

    return inSampleSize;
}

Logcat

04-13 19:29:47.868 3973-3973/com.yang.wallpaperchanger I/GalleryActivity: null

我在stackoverflow和其他网站上做过研究,但仍然无法获得解决方案。感谢你的帮助!

0 个答案:

没有答案