【问题标题】:How can I store JPEGs as Byte[] for faster loading?如何将 JPEG 存储为 Byte[] 以加快加载速度?
【发布时间】:2012-09-16 19:38:37
【问题描述】:

我需要将大约 150 个 JPEG 图像加载到 ArrayList 中以播放动画。

如果我这样加载它们

ByteArrayOutputStream stream = new ByteArrayOutputStream();
BitmapFactory.decodeResource(getResources(), R.drawable.y1).compress(Bitmap.CompressFormat.JPEG, 80, stream);
byeArr.add( stream.toByteArray() );

150 张图片最多可能需要 10 秒,所以也许有办法加快速度?我可以以某种方式将这些图像存储在资源或资产中已经作为 byte[] 或其他东西吗?

谢谢

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以使用以下方法从资源中获取原始数据。您无需解码然后再次压缩。

    byte[] getBytesFromResource(final int res) {
        byte[] buffer = null;
        InputStream input = null;
    
        try {
            input = getResources().openRawResource(res);
            buffer = new byte[input.available()];
            if (input.read(buffer, 0, buffer.length) != buffer.length) {
                buffer = null;
            }
        } catch (IOException e) {
            buffer = null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {}
            }
        }
    
        return buffer;
    }
    

    【讨论】:

    • @RogerTravis 你可以接受答案然后:) 你不能在解码图像时逃避解码时间,除了这个技术应该可以帮助你。如果解码是 jpeg 压缩等的缓慢播放。这可能会有所帮助。
    猜你喜欢
    • 2017-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-26
    • 2013-04-03
    相关资源
    最近更新 更多