首页 > 解决方案 > 将 CustomView 保存为位图

问题描述

我来自以下问题(我问过): 将当前视图保存为位图

为了提出这个问题,我会提供尽可能多的细节。

我的最终目标是编写一个包含一些信息的视图(稍后将来自 API,可能是包含大量文本的 JSOn 对象)。到目前为止我所做的是:1)创建一个自定义视图 2)在这个自定义视图上绘制一个带有我需要的信息的画布(canvas.drawText()) 3)把这个 CustomView 放在 activity_main.xml 上(参考它) 4) 在 MainActivity.kt 上实例化这个 CustomView (现在问题开始了) 5) 将这个 CustomView 转换为 Bitmap (使用扩展方法。6) 将转换后的 CustomView 保存到 SD 卡

但是,当我尝试保存它时,什么也没有发生。没有创建文件夹,LogCat 窗口上也没有任何内容(我正在检查文件\文件夹是否是使用 Android Studio 上的设备文件资源管理器创建的)。

阅读后我明白我应该有一个 ViewTreeObserver 来观察变化(例如:然后视图完成绘制)。我将此作为扩展方法添加到我的代码中(在 SO 上找到但现在找不到链接)但也没有改变。

为了将位图保存到内部存储,我从以下链接获得了方法: https ://android--code.blogspot.com/2018/04/android-kotlin-save-image-to-internal.html (我只是调整了该方法,因为我需要使用位图而不是可绘制的)。

我错过了什么吗?据我所见,我正在做正确的事情来将位图保存到 SD。(问题很大,因为我发布的代码)信息:使用 Android Studio 3.5.1 Kotlin 语言

我的activity_main.xml

   <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <com.example.desenhanota.CustomView
android:id="@+id/MyCustomview"
            android:layout_width="match_parent"
            android:layout_height="442dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />


    </RelativeLayout>

ViewTreeObserver 扩展方法:

inline fun View.doOnGlobalLayout(crossinline action: (view: View) -> Unit) {
    val vto = viewTreeObserver
    vto.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
        @SuppressLint("ObsoleteSdkInt")
        @Suppress("DEPRECATION")
        override fun onGlobalLayout() {
            action(this@doOnGlobalLayout)
            when {
                vto.isAlive -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        vto.removeOnGlobalLayoutListener(this)
                    } else {
                        vto.removeGlobalOnLayoutListener(this)
                    }
                }
                else -> {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                        viewTreeObserver.removeOnGlobalLayoutListener(this)
                    } else {
                        viewTreeObserver.removeGlobalOnLayoutListener(this)
                    }
                }
            }
        }
    })
}

自定义视图文件 (CustomView.kt)

class CustomView @JvmOverloads constructor(
        context: Context,
        attrs: AttributeSet? = null, 
        defStyleAttr: Int = 0
): View(context, attrs, defStyleAttr) {

    private val  textoGeral = Paint()

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        textoGeral.setColor(Color.BLACK)

        canvas?.drawText("DRAW TEST ON CANVAS TEST TEST ", 0f, 120f, textoGeral)
    }
}

主要活动

class MainActivity : AppCompatActivity() {

    private val TAG = "MainActivity"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val outraView = CustomView(this)
        outraView.doOnGlobalLayout {
            try {
                val bmp = outraView.fromBitmap()
                val uri: Uri = saveImageToInternalStorage(bmp)

            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
    }

    private fun saveImageToInternalStorage(bitmap :Bitmap):Uri{
        // Get the context wrapper instance
        val wrapper = ContextWrapper(applicationContext)
        // The bellow line return a directory in internal storage
        var file = wrapper.getDir("images", Context.MODE_PRIVATE)
        file = File(file, "${UUID.randomUUID()}.jpg")
        try {
            val stream: OutputStream = FileOutputStream(file)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
            stream.flush()
            stream.close()
        } catch (e: IOException){ // Catch the exception
            e.printStackTrace()
        }
        // Return the saved image uri
        return Uri.parse(file.absolutePath)
    }
}

编辑 1:我更改了用户 mhedman 对评论的建议。他提到我正在访问我的自定义视图的一个新实例,而不是已经从活动布局中绘制的那个。当我在 ViewTreeObsever 事件之外尝试时,我有一个异常说“宽度和高度必须> 0”。在 ViewTreeObserver 内部,什么也没有发生(不显示任何消息)。

使用建议更新代码:

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

            val outraView = MyCustomView
        outraView.doOnGlobalLayout {
            val finalBmp = outraView.fromBitmap()
            val uri: Uri = saveImageToInternalStorage(finalBmp)
        }

标签: androidkotlinbitmapandroid-view

解决方案


最后我让它工作了。感谢所有支持并提供意见的用户,我找出了问题所在并使其正常工作。老实说,当 mhemdan 说我正在创建一个新视图而不是使用我在 activitiy_main.xml 上已有的视图时,他提到了关键的事情。从那里我只是做了一些调整,终于让它工作了。让我分享最终的代码。

activity_main.xml(我添加了一个按钮来触发截屏动作。仅此而已)。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnShare"
        android:layout_marginTop="10dp"
        android:text="Share"/>

    <com.example.notasdraw.CustomView
        android:id="@+id/MyCustomview"
        android:layout_width="match_parent"
        android:layout_height="442dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />



</RelativeLayout>

自定义视图代码没有任何变化。

MainActivity.kt(事情发生了变化)

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        try
        {
            val button = findViewById<Button>(R.id.btnShare)
            button?.setOnClickListener {
                val bmpFromView = getScreenShot(MyCustomview) //Taking screenshot of the view from activity_main.xml
                val finalPath = saveImageToInternalStorage(bmpFromView) //Saving it to the sd card
                toast(finalPath.toString()) //Debug thing. Just to check the view width (so i can know if its a valid view or 0(just null))
            }

        }
        catch(e: Exception)
        {
            e.printStackTrace()
        }

    }
    private fun saveImageToInternalStorage(bitmap :Bitmap):Uri{
        // Get the context wrapper instance
        val wrapper = ContextWrapper(applicationContext)
        // The bellow line return a directory in internal storage
        var file = wrapper.getDir("images", Context.MODE_PRIVATE)
        file = File(file, "${UUID.randomUUID()}.jpg")
        try {
            val stream: OutputStream = FileOutputStream(file)
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream)
            stream.flush()
            stream.close()
        } catch (e: IOException){ // Catch the exception
            e.printStackTrace()
        }
        // Return the saved image uri
        return Uri.parse(file.absolutePath)
    }
    fun getScreenShot(view: View): Bitmap { //A few things are deprecated but i kept them anyway
        val screenView = view.rootView
        screenView.isDrawingCacheEnabled = true
        val bitmap = Bitmap.createBitmap(screenView.drawingCache)
        screenView.isDrawingCacheEnabled = false
        return bitmap
    }

    }
    fun Context.toast(message: String) { //Just to display a toast
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

我仍然会改进这段代码(删除不推荐使用的东西等)。但就目前而言,这可以完成这项工作。谢谢。


推荐阅读