繁体   English   中英

BitmapFactory.Decode返回null

[英]BitmapFactory.Decode returns null

谢谢阅读。

[编辑]如果我省略选项,即

Bitmap  bmp = BitmapFactory.decodeFile(picturePath); 

然后,它返回一个位图。

但是以选项作为参数,即

 bmp = BitmapFactory.decodeFile(picturePath, options);

它返回null。

任何人都可以知道选择权的缺失或错误之处吗?

再次感谢,

[编辑结束]

目的是选择一个图像,如果需要对其进行下采样,保存,然后将其加载到图像视图中。

BitmapFactory解码始终返回null。 我确实在清单中设置了权限,路径显示为完整。./storage/emulated/0/aerg.png

见下文:

if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
    {

        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();



        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        String imageType = options.outMimeType;



        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;

        BitmapFactory.decodeFile(picturePath, options);

        int inSampleSize = 1;

        if (height > mheight || width > mWidth)
        {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while ((halfHeight / inSampleSize) > mheight
                    && (halfWidth / inSampleSize) > mWidth)
            {
                inSampleSize *= 2;
            }
        }

        options.inSampleSize = inSampleSize;

        Bitmap bmp = BitmapFactory.decodeFile(picturePath,options);

        String downsampledPicturePath = saveImage(bmp, picturePath, "PrinterImages");

        Bitmap b = BitmapFactory.decodeFile(downsampledPicturePath);
        iv.setImageBitmap(b);

        //ImageView imageView = (ImageView) findViewById(R.id.theImageView);
        //imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
}

private String saveImage(Bitmap bmp, String path, String folderName)
{
    Context context = getApplicationContext();
    File folder = context.getDir(folderName, Context.MODE_PRIVATE); //Creating an internal dir;

    String pictureName = path.substring(path.lastIndexOf("/") + 1);
    String picture = new File(folder, pictureName).getAbsolutePath();

    FileOutputStream fos = null;
    try
    {
        fos = new FileOutputStream(picture);

        // Use the compress method on the BitMap object to write image to the OutputStream
        bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
        fos.close();
    } catch (Exception e)
    {
        e.printStackTrace();
    }
    return picture;

}

我将从解码文件的文档中开始,因为这显然非常重要。

公共静态Bitmap解码文件(字符串路径名,BitmapFactory.Options选择)

API级别1中添加了将文件路径解码为位图。 如果指定的文件名为null,或者无法将其解码为位图,则该函数返回null。

参数pathName要解码的文件的完整路径名。 选择null-ok; 控制下采样以及是否应完全解码图像或仅返回尺寸的选项。 返回解码后的位图;如果无法解码图像数据,则为null;或者,如果opts为非null,则如果opts请求仅返回大小(在opts.outWidth和opts.outHeight中)。

我对这个描述不太了解,但是在我看来,您从来没有通过查看代码来在选项中设置宽度或高度? 我可能对此完全不满意。

另请注意; http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html

“ public boolean inJustDecodeBounds如果设置为true,则解码器将返回null(无位图),但输出...”

我已经在旅途中写了这个回复。 我认为上面的引用是您的问题-您在代码中将其设置为true。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM