将整个布局转换为图像/位图 [英] Converting an entire layout to an image/bitmap

查看:104
本文介绍了将整个布局转换为图像/位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这段时间我一直对这行代码有疑问,如果你们当中有人对它有想法,我将不胜感激.

I have been having an issue with this one line of code for a while and I would really apreciate it if any of you have ideaas about it.

我想做的是,当一个人按下按钮时,将特定的布局(在这种情况下为相对布局)保存为图像.此操作会将图像转移到下一个活动.我很确定转移的实际行为不是问题.这就是我将布局转换为图像的方式.下面是带有注释的代码:

What I am trying to do is, save a specific layout( in this case a relative layout) as an image when a person hits a button. this action will transfer the image to the next activity. I'm pretty sure the actual act of transfering is not the problem. It's how I'm converting the layout to an image. below is the code with comments:

这是Activity1上的内容

        // here I found the button I wanted to use and made it:
        //   1. take us to next activity
        //   2. convert my relativelayout to and a bitmap and store it in a variable for transfer.
        
        findViewById<Button>(R.id.DoneButton).setOnClickListener{
            val intent = Intent(this, SaveAndNameActivity::class.java)

            //here is where I converted my relative layout to a bitmap. 
            val pictureStackCompleted =  findViewById<RelativeLayout>(R.id.pictureStack).drawToBitmap()


            //here is where I prepared it for transfer to the next activity (NOTE!: the value here is Parceable? , I chose this bcause it works with bitmaps)
          intent.putExtra("pictureStackCompleted" , pictureStackCompleted)
            
            //here is where I told activity2 to start
            startActivity(intent)



        }```

这是Activity2上的内容

        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_save_and_name)

        //here is where I called the variable containing, my relative layout turned bitmap image
        val completedPicture = intent.getParcelableExtra<Bitmap>("pictureStackCompleted")

        //here I set my image to be the background image of an ImageView on activity2
       val finalImage = findViewById<ImageView>(R.id.finalImage)
        finalImage.setImageBitmap(completedPicture)
    }

我100%确信它与位图转换行有关,因为我通过将代码行移到应用程序的另一部分(工作正常)来测试了该行代码.当我运行该应用程序时,工作部件不再起作用.

I am 100% sure it has somehting to do with the bitmap conversion line because I tested that line of code by moving it out of intent to another part of the app ( which worked fine). when I ran the app that working part no longer functioned.

推荐答案

原因是您正在尝试使用Intent传递Bitmap.尽管从理论上讲是可能的,但Bitmap的大小会导致出现问题,如您的错误所示:

The reason is you're trying to pass a Bitmap around using an Intent. Although it's theoretically possible, the size of the Bitmap causes a problems as shown by your error:

E/JavaBinder: !!! FAILED BINDER TRANSACTION !!! (parcel size = 3602192)

如果您要将Bitmap传递给另一个活动,那么我建议:

If you want to pass an Bitmap to another activity, then I'd suggest:

  1. Bitmap保存到文件

将这些添加到您的manifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

然后下面的代码应该起作用

Then the below code should work

File bitmapFile = new File(Environment.getExternalStorageDirectory() + "MyBitmap" + ".png");
FileOutputStream fos = new FileOutputStream(bitmapFile);  
bitmap.compress(Bitmap.CompressFormat.PNG, 80, fos);
fos.flush();
fos.close();  

  • 使用Intent/Bundle

    intent.putExtra("filename", "MyBitmap.png");
    

  • 在下一个活动中使用文件路径重新创建Bitmap

    String fileName = getIntent().getStringExtra("filename");
    String filePath = Environment.getExternalStorageDirectory() + fileName;
    Bitmap bitmap = BitmapFactory.decodeFile(filePath);
    

  • 这篇关于将整个布局转换为图像/位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆