有什么方法可以提高BitmapFactory.decodeStream()的速度?

Lux*_*ode 6 android

显然,这是一项昂贵/耗时的操作.有什么方法可以改善吗?

Bitmap bm = BitmapFactory.decodeStream((InputStream) new URL(
                                            someUrl).getContent());
Run Code Online (Sandbox Code Playgroud)

我猜测真的没有办法避免这种相对激烈的操作,但想知道是否有人有任何调整他们可以推荐(除了缓存实际的位图,无论出于何种原因,这里都没有相关性)

Mar*_*jøl 6

如果你不需要全分辨率,你只能读取第n个像素,其中n是2的幂.你可以通过设置传递给inSampleSize选项对象来实现BitmapFactory.decodeFile.您可以通过inJustDecodeBoundsOptions对象上设置第一遍中从文件中读取元数据来查找样本大小.除此之外 - 不,我认为有一种简单的方法可以让它比现在更快.

编辑,示例:

    Options opts = new Options();
    // Get bitmap dimensions before reading...
    opts.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(path, opts);
    int width = opts.outWidth;
    int height = opts.outHeight;
    int largerSide = Math.max(width, height);
    opts.inJustDecodeBounds = false; // This time it's for real!
    int sampleSize = ??; // Calculate your sampleSize here
    opts.inSampleSize = sampleSize;
    Bitmap bmp = BitmapFactory.decodeFile(path, opts);
Run Code Online (Sandbox Code Playgroud)