位图总是在android中返回null

时间:2012-08-04 14:05:31

标签: java android bitmap null gallery

我一直在尝试创建一个小型图库,位图始终返回null。代码是这样的:

    public View getView(int position, View convertView, ViewGroup parent) {

        ImageView i = new ImageView(mContext);
        //Toast.makeText(getApplicationContext(),imgArray2.length+ " Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
        //Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position]);
        //Uri uri = Uri.parse(imgArray2[position]);
        //Bitmap bitmap = decodeFile(new File(uri.toString()).getAbsoluteFile());
        //Bitmap bitmap = BitmapFactory.decodeFile(uri.toString());
        //int imgID = getResources().getIdentifier(path, "drawable", "mypack.pack");
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 15;
        Bitmap bitmap = BitmapFactory.decodeFile(imgArray2[position], options);
        //i.setImageResource(imgArray2[position]);
        i.setImageBitmap(bitmap);
        //Uri uri = Uri.parse(imgArray2[position]);
        Toast.makeText(getApplicationContext(), "Image path from gallery : " + imgArray2[position], Toast.LENGTH_SHORT).show();
        //i.setImageURI(Uri.parse(imgArray2[position]));
        i.setLayoutParams(new Gallery.LayoutParams(170, 170));
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        i.setBackgroundResource(mGalleryItemBackground);
        return i; 
   }

正如您在注释掉的代码中所看到的,我一直在尝试很多选项。 setImageURI有效但我需要缩小图像,因为我有很多。图像在SD卡中。我检查了图像的路径,这是正确的。这里有什么问题?

1 个答案:

答案 0 :(得分:0)

您是否分析了日志?有时,当图像太大而无法解码时,Dalvik VM将拒绝内存请求,从而导致返回空图像。