【问题标题】:Why the bitmap from res/raw folder is null?为什么 res/raw 文件夹中的位图为空?
【发布时间】:2013-11-21 12:00:31
【问题描述】:

我尝试从 res/raw 文件夹中解码位图(1920x1920),这是一个 png 资源。我需要全尺寸位图。我想使用 BimtapFactory.decodeFileDescriptor 而不是 BitmapFactory.decodeFile||decodeResource 因为比其他人处理 OOM 的可靠性更高: 当我尝试这段代码时,位图为空!!!但该文件不为空。我一直在努力打球。 请帮忙!

        Context mCtx=MainActivity.this;
        Bitmap bm = null;             
        //id is the resId in res/raw
        AssetFileDescriptor file =mCtx.getResources().openRawResourceFd(R.raw.skin_default_0);
        bm = BitmapFactory.decodeFileDescriptor(file.getFileDescriptor(), null,
                options);

【问题讨论】:

    标签: android bitmap


    【解决方案1】:
    InputStream bm = getResources().openRawResource(R.raw.splash);
        BufferedInputStream bufferedInputStream = new BufferedInputStream(bm);
        Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);
        int nh = (int) (bmp.getHeight() * (512.0 / bmp.getWidth()));
        bmp = Bitmap.createScaledBitmap(bmp, 512, nh, true);
        view.setImageBitmap(bmp);
    

    试试这个代码。

    原始文件夹 - 保持文件未压缩。 我不这么认为,与BitmapFactory.decodeStream 相比,BitmapFactory.decodeFileDescriptor 会给你有效的结果,获取文件的 inputStream 并对其进行解码。如果您认为图像太大并且您使用许多像这样的图像,您的应用程序将会非常繁重,因为原始文件夹文件不会被压缩。如果您想缩放位图,请使用我在代码中添加的 sn-p 代码。我想会给你一个更好的理解。

    谢谢...

    【讨论】:

    • 据说 (stackoverflow.com/questions/477572/…) decodeFileDescriptor 调用与 decodeStream/decodeFile 不同的本地方法。我只想将 square(1920*1920) 图像剪辑到 1920*1080 而没有 OOM 。有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2014-10-05
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多