java - 位图在android中总是返回null

标签 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卡里。我检查了图像的路径,它是正确的。这里做错了什么?

最佳答案

你分析过日志吗?有时,当图像太大而无法解码时,Dalvik VM 会拒绝内存请求,导致返回空图像。

关于java - 位图在android中总是返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11809251/

相关文章:

java - 如何显示带有自定义时区的日期?

android - 启动器图标(仅限旧版)显示白色背景图标 android studio 3.3

c - 使用 C 进行 24 位 bmp 边缘检测

java - Android java、位图变量内存使用

java - 如何从 LoggingEvent 获取 ISO 格式的时间?

java - 如何从日期字符串添加 GMT

java - 在Java中打印随机数

android - 如何使用 RelativeLayout 将按钮对齐到 ImageView 的右侧

java - 改善 Android 应用程序 fragment 交易的加载时间

html - Canvas 脆皮文字渲染