如何在Android Emulator中复制OutOfMemory?

如何在Android Emulator中复制OutOfMemory?,android,android-emulator,out-of-memory,android-bitmap,Android,Android Emulator,Out Of Memory,Android Bitmap,我正在开发的应用程序通过了物理设备的测试过程。测试小组报告说 华为P8 Lite Android 5.1:如果上传的图像超过4MB,应用程序会意外关闭 我试图用“谷歌Pixel3A安卓5.1”仿真器复制这个问题,但过程顺利。这个上传过程的代码包括一个调整大小的方法,这样无论你选择什么图像,当它到达服务器时都不会超过4MB。 我需要重现错误并找到解决问题的方法,或者对代码做些什么来防止这种崩溃(我们认为这是内存不足崩溃),不管设备有多旧。 我们支持安卓21+ private fun resize(

我正在开发的应用程序通过了物理设备的测试过程。测试小组报告说

华为P8 Lite Android 5.1:如果上传的图像超过4MB,应用程序会意外关闭

我试图用“谷歌Pixel3A安卓5.1”仿真器复制这个问题,但过程顺利。这个上传过程的代码包括一个调整大小的方法,这样无论你选择什么图像,当它到达服务器时都不会超过4MB。 我需要重现错误并找到解决问题的方法,或者对代码做些什么来防止这种崩溃(我们认为这是内存不足崩溃),不管设备有多旧。 我们支持安卓21+

private fun resize(image: Bitmap, maxWidth: Int, maxHeight: Int): Bitmap {
        var imageResized = image
        if (maxHeight > 0 && maxWidth > 0) {
            val width = imageResized.width
            val height = imageResized.height
            val ratioBitmap = width.toFloat() / height.toFloat()
            val ratioMax = maxWidth.toFloat() / maxHeight.toFloat()

            var finalWidth = maxWidth
            var finalHeight = maxHeight
            if (ratioMax > ratioBitmap) {
                finalWidth = (maxHeight.toFloat() * ratioBitmap).toInt()
            } else {
                finalHeight = (maxWidth.toFloat() / ratioBitmap).toInt()
            }
            imageResized = Bitmap.createScaledBitmap(imageResized, finalWidth, finalHeight, true)
            return imageResized
        } else {
            return imageResized
        }
    }

    fun captureImageResult(requestCode: Int, data: Intent?) {
    if (requestCode == SELECT_PICTURE) {
        data?.data?.let {
            mCapturedImageURI = data.data
        }
    }


    mCapturedImageURI.let {
        val msm = MediaStorageManager((view as NewBaseFragment).activity!!)
        val bmp = msm.getBitmapFromUri(mCapturedImageURI!!)

        //save image bytes
        imageBytes = msm.getBytesFromBitmap(bmp!!)

        val bitmapScaled = resize(bmp, 1024, 1024)
        imageBytes = msm.getBytesFromBitmap(bitmapScaled)

        //image too big
        if (imageBytes?.size ?: IMAGE_MAX_SIZE < IMAGE_MAX_SIZE) {
            //show image selected
            view.renderImage(bitmapScaled)
        } else {
            view.showErrorDialog()
            imageBytes = null
        }
    }
}
private fun resize(图像:位图,maxWidth:Int,maxHeight:Int):位图{
var imageResized=image
如果(maxHeight>0&&maxWidth>0){
val width=imageResized.width
val height=图像大小调整。高度
val ratioBitmap=width.toFloat()/height.toFloat()
val ratioMax=maxWidth.toFloat()/maxHeight.toFloat()
var finalWidth=maxWidth
var finalHeight=maxHeight
如果(比率最大值>比率最大值){
finalWidth=(maxHeight.toFloat()*ratioBitmap.toInt())
}否则{
最终高度=(maxWidth.toFloat()/ratioBitmap.toInt())
}
imageResized=Bitmap.createScaledBitmap(imageResized,finalWidth,finalHeight,true)
返回已调整大小的图像
}否则{
返回已调整大小的图像
}
}
有趣的captureImageResult(请求代码:Int,数据:Intent?){
if(requestCode==选择图片){
数据?,数据?,让我来{
mCapturedImageURI=data.data
}
}
mCapturedImageURI.let{
val msm=MediaStorageManager((以NewBaseFragment的形式查看)。活动!!)
val bmp=msm.getBitmapFromUri(mCapturedImageURI!!)
//保存图像字节
imageBytes=msm.getBytesFromBitmap(bmp!!)
val bitmapScaled=调整大小(bmp,1024,1024)
imageBytes=msm.getBytesFromBitmap(位图缩放)
//图像太大
if(imageBytes?.size?:图像最大大小<图像最大大小){
//显示选定的图像
视图.渲染图像(位图缩放)
}否则{
view.downRorDialog()
imageBytes=null
}
}
}

将此添加到您的menifest android中:largeHeap=“true”将此添加到您的menifest android中:largeHeap=“true”