Search code examples
androidandroid-studiokotlinandroid-activitybitmap

Converting an entire layout to an image/bitmap


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:

Here is whats on 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)



        }```

Here is whats on 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)
    }

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.


Solution

  • 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)

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

    1. Save the Bitmap to a file

      Add these to your 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();  
      
    2. Send the filepath as a String to the next activity using Intent/Bundle

      intent.putExtra("filename", "MyBitmap.png");
      
    3. Use the filepath in the next activity to recreate the Bitmap

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