繁体   English   中英

将高质量图像加载到imageview

[英]Load High quality image to imageview

我想将15Mb图像文件加载到imageview中。我尝试使用piccasa和其他方式加载它,但是所有这些都返回内存不足异常。

我使用的代码是

       Picasso.with(getActivity()).load(R.drawable.highqual).into(imageView);

有什么办法可以使它成为可能

尝试这个:

调用此函数decodeSampledBitmapFromFile(String filePath,int reqWidth, int reqHeight)

其中reqHeightreqWidth是您的ImageView尺寸。 还要注意,如果花费很长时间并发送给您ANR ,请在单独的thread上运行它,然后将其设置为ImageView

该代码来自我将其更改为读取图像文件的doc。

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        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) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
    }

    public static Bitmap decodeSampledBitmapFromFile(String filePath,int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(filePath, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(filePath, options);
    }

参考:

有效地加载大位图

暂无
暂无

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

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