【问题标题】:Android Load Large Image For PrintingAndroid加载大图像进行打印
【发布时间】:2018-06-03 03:41:53
【问题描述】:

我正在开发一款设计应用。它需要创建一个非常大的不可见视图,其中包含图像,然后将其转换为位图以进行打印,但 Android 不允许我将如此巨大的图像加载到内存中。有什么解决办法吗?

【问题讨论】:

    标签: android printing bitmap out-of-memory


    【解决方案1】:

    您不必采取隐形视图并在其中加载图像。您可以直接在图像视图中加载图像并将其转换为位图,这是我的代码,我在 XML 中加载图像,然后将整个 XML 文件转换为位图,因为我还有很多其他的东西要在 Bitmap.I 中转换希望这段代码对你有所帮助。

    RawMapInfoWindowBinding rawMapInfoWindowBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.raw_map_info_window, (ConstraintLayout) mBinding.getRoot(), false);

    rawMapInfoWindowBinding.clLike.measure( View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED), View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)); rawMapInfoWindowBinding.clLike.layout(0, 0, rawMapInfoWindowBinding.clLike.getMeasuredWidth(), rawMapInfoWindowBinding.clLike.getMeasuredHeight());

    我使用 glide 来加载我的图片

    enter code here
    
        try {
    
            GlideApp.with(this).asBitmap().centerCrop().circleCrop().load(statusModel.profileurl)
                    .diskCacheStrategy(DiskCacheStrategy.ALL).listener(new RequestListener<Bitmap>() {
                @Override
                public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Bitmap> target, boolean isFirstResource) {
                    LogUtils.LOGE(TAG, "onLoadFailed: " + statusModel.statusID);
                    Log.e(TAG, "onLoadFailed --------- " + isSearching);
                    return false;
                }
    
                @Override
                public boolean onResourceReady(Bitmap bitmap, Object model, Target<Bitmap> target, DataSource dataSource, boolean isFirstResource) {
               rawMapInfoWindowBinding.ivProfileImage.setImageBitmap(bitmap);
               return true;
                }
            }).submit(100, 100);
    
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }`
    

    【讨论】:

    • 问题是打印需要高质量的图像,但我无法将大小高达 20Mb 的巨大图像加载到内存中
    • 为什么需要在位图中转换图像?
    • 因为我需要让用户进行一些修改
    猜你喜欢
    • 2015-12-06
    • 2017-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 2012-09-25
    相关资源
    最近更新 更多