繁体   English   中英

如何将片段布局转换为 bitmap 并通过 email 发送而不保存任何文件(Kotlin 2022)?

[英]How to convert a Fragment layout to a bitmap and send it by email without saving any file (Kotlin 2022)?

我正在使用 Kotlin 开发 Android 应用程序。 我需要转换一个布局(这实际上是用户输入数据的报告,一些视图在图像视图和额外文本中转换为 bitmap)并由 email as.Z437175BA4191210EE004E1D93744内的用户单击按钮时发送未来 PDF 的预可视化)而不保存它

我已经阅读并观看了数小时的教程,但没有任何帮助(已弃用,旧 java 或保存文件)

我想我需要一个 function 将所需视图转换为 bitmap 图像,将其缩放为 A4 尺寸格式,将缩放后的图像设置为 Z437175BA4191210EE004E1D937494D937494D937494D937494D937494D937494D09Z片段内。

我会更新帖子。 谢谢

编辑:我已经知道如何将布局转换为 bitmap 并将其放入图像视图中(如果有人需要,我可以分享)。 So I only need a function that takes the bitmap as an argument and returns.pdf (or.jpg) and an other one that intents as attachment that returned pdf to send email

我收集到的内容,您需要创建各种视图的屏幕截图。 这是我不久前做的事情,所以我有一些 java 代码,尚未转换为 Kotlin,但 AS 可以解决这个问题。

您可以使用 class PixelCopy ,不幸的是它是 Android O +。

这是您可以执行的操作的简化版本:

int[] viewLocationInWindow = new int[2];
view.getLocationInWindow(viewLocationInWindow);

int x = viewLocationInWindow[0];
int y = viewLocationInWindow[1];

int width = view.getWidth();
int height = view.getHeight();

Rect rectScope = new Rect(x, y, x + width, y + height);

Bitmap screenshotBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Handler handler = new Handler();
        
PixelCopy.request(getWindow(), rectScope, screenshotBitmap, copyResult -> {
            if (copyResult == PixelCopy.SUCCESS) {
                listener.onScreenshotSuccess(screenshotBitmap);
            } else {
                listener.onScreenshotError();
            }
            handler.removeCallbacksAndMessages(null);
        }, handler);

此处引用的view应该是您的包含视图。 就我而言,我必须截取整个视图(整个屏幕)的屏幕截图,所以我做了:

int width = Resources.getSystem().getDisplayMetrics().widthPixels;
int height = Resources.getSystem().getDisplayMetrics().heightPixels;

然后只需实现监听器

public interface OnScreenshotTakenListener {
        void onScreenshotSuccess(Bitmap bitmap);
        
        void onScreenshotError();
    }

onScreenshotSuccess中,您可以修改 bitmap 并满足您的所有其他要求。

fun getDetailImage(){

    val image=getBitmapFromView(llParent)
    ivShareImage.setImageBitmap(image)
}

private fun getBitmapFromView(view: View): Bitmap? {

    //Define a bitmap with the same size as the view
    val returnedBitmap=Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
    //Bind a canvas to it
    val canvas=Canvas(returnedBitmap)
    //Get the view's background
    val bgDrawable=view.background
    if (bgDrawable != null) {
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas)
    } else {
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE)
    }
    // draw the view on the canvas
    view.draw(canvas)
    //return the bitmap
    return returnedBitmap
}

fun getShareImage(){
    getDetailImage()
    val mDrawable: Drawable=ivShareImage.getDrawable()
    val mBitmap=(mDrawable as BitmapDrawable).bitmap

    val path=MediaStore.Images.Media.insertImage(
        appCompatActivity.getContentResolver(),
        mBitmap,
        "image-name",
        null
    )
    val uri: Uri=Uri.parse(path)
    val shareIntent=Intent()
    shareIntent.action=Intent.ACTION_SEND
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
    shareIntent.type="image/*"
    appCompatActivity.startActivity(Intent.createChooser(shareIntent, "Share Image"))
}

所以我改编并测试了 Umesh Maharjan 的回答

fun getBitmapFromView(view: View): Bitmap? {
            //Define a bitmap with the same size as the view
            val returnedBitmap=Bitmap.createBitmap(view.width, view.height, Bitmap.Config.ARGB_8888)
            //Bind a canvas to it
            val canvas= Canvas(returnedBitmap)
            //Get the view's background
            val bgDrawable=view.background
            bgDrawable.draw(canvas)
            // draw the view on the canvas
            view.draw(canvas)
            //return the bitmap
            return returnedBitmap
        }

        fun getShareImage(){
            val image = getBitmapFromView(binding.layoutPdf)
            val mDrawable: Drawable = image!!.toDrawable(resources)
            val mBitmap=(mDrawable as BitmapDrawable).bitmap

            val path= MediaStore.Images.Media.insertImage(
                requireActivity().contentResolver,
                mBitmap,
                "image-name",
                null
            )
            val uri = Uri.parse(path)
            val shareIntent= Intent()
            shareIntent.action=Intent.ACTION_SEND
            shareIntent.putExtra(Intent.EXTRA_STREAM, uri)
            shareIntent.type="image/*"
            requireActivity().startActivity(Intent.createChooser(shareIntent, "Share Image"))
        }

但似乎MediaStore.Images.Media.insertImage()已被弃用。 我收到错误java.lang.NullPointerException: uriString

暂无
暂无

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

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