0

I am working on Android application, where I am creating a QR code by using the QR generation library. I have achieved the QR code generation as by sending data from previews Activity to the main Activity where I have generated and shown the QR code.

The issue takes place in the process of saving QR code to the gallery and sharing the same QR code.

I have implemented sharing intent but it says the intent cannot share an empty file.

The same issue appears when I try to save the file.

Basically file with the QR code is always empty.

Here is my code for sharing and QR generation:

    class QRGenerationAll : AppCompatActivity() {

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

        val byteArray = intent.getByteArrayExtra("logoimage")
        val bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
        img_logoimage.setImageBitmap(bmp)

        val valuecatchedvalues: String = intent.getStringExtra("sendedvalues")
        tv_textforqr.setText(valuecatchedvalues)

        val tv_textforqr: String = intent.getStringExtra("logotext")
        tv_textforlogoname.setText(tv_textforqr)


        try {
            //setting size of qr code
            val manager = getSystemService(WINDOW_SERVICE) as WindowManager
            val display = manager.defaultDisplay
            val point = Point()
            display.getSize(point)
            val width = point.x
            val height = point.y
            val smallestDimension = if (width < height) width else height
            // val qrInput = findViewById(R.id.qrInput) as EditText

            //setting parameters for qr code
            val charset = "UTF-8" // or "ISO-8859-1"
            val hintMap: MutableMap<EncodeHintType, ErrorCorrectionLevel> = HashMap()
            hintMap[EncodeHintType.ERROR_CORRECTION] = ErrorCorrectionLevel.L

            if (valuecatchedvalues!=null) {

                createQRCodeText(
                    valuecatchedvalues,
                    charset,
                    hintMap,
                    smallestDimension,
                    smallestDimension
                ) //MAIN METHOD FOR QR GENERATE
            }
        } catch (ex: Exception) {
            Log.e("QrGenerate", ex.message)
        }

        buttonshare.setOnClickListener(View.OnClickListener {
            val sharingIntent = Intent(Intent.ACTION_SEND)
            sharingIntent.type = "text/plain"
            sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject Here")
            sharingIntent.putExtra(Intent.EXTRA_TEXT, bmp)
            startActivity(
                Intent.createChooser(
                    sharingIntent,
                    resources.getString(R.string.app_name)
                )
            )
        })


    }

    private fun createQRCodeText(
        valueqrTextData: String,
        charset: String,
        hintMap: MutableMap<EncodeHintType, ErrorCorrectionLevel>,
        smallestDimension: Int,
        smallestDimension1: Int
    ) {

        try {
            //generating qr code in bitmatrix type
            val matrix = MultiFormatWriter().encode(
                String(
                    valueqrTextData.toByteArray(charset(charset)), kotlin.text.charset(charset)
                ), BarcodeFormat.QR_CODE, smallestDimension, smallestDimension1, hintMap
            )
            //converting bitmatrix to bitmap
            val width = matrix.width
            val height = matrix.height
            val pixels = IntArray(width * height)
            // All are 0, or black, by default
            for (y in 0 until height) {
                val offset = y * width
                for (x in 0 until width) {
                    pixels[offset + x] = if (matrix[x, y]) Color.BLACK else Color.WHITE
                }
            }
            val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
            bitmap.setPixels(pixels, 0, width, 0, 0, width, height)
            //setting bitmap to image view
            val myImage = findViewById(R.id.imageView1) as ImageView
            myImage.setImageBitmap(bitmap)
        } catch (er: java.lang.Exception) {
            Log.e("QrGenerate", er.message)
        }

    }
}

The following snippet is related to extracting text from previous Activity and generating QR in the receiving Activity.

    val byteArray = intent.getByteArrayExtra("logoimage")
    val bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.size)
    img_logoimage.setImageBitmap(bmp)

    val valuecatchedvalues: String = intent.getStringExtra("sendedvalues")
    tv_textforqr.setText(valuecatchedvalues)

    val tv_textforqr: String = intent.getStringExtra("logotext")
    tv_textforlogoname.setText(tv_textforqr)

There is the only issue I am Facing here. I have QR image generated but as I try to share and save it it seems impossible for me.

Here is the Message after Share intentImage

I have searched this link for guidance Link 1

Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
Usman Ali
  • 425
  • 1
  • 9
  • 31
  • Please dont post two problems. Or post code to save data to file. Or post code that tries to share a file. – blackapps Oct 06 '20 at 11:14

1 Answers1

0

You've passed the wrong argument value for EXTRA_TEXT key. Instead of bmp you should use valuecatchedvalues:

sharingIntent.putExtra(Intent.EXTRA_TEXT, valuecatchedvalues)

Why?

  1. Because this is the value you create your QR code from;
  2. bmp is of type Bitmap. Bitmap cannot be processed when the value for EXTRA_TEXT key is extracted because String is expected.
Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
  • i want the QR That is Generatesd by Passed value to be shared but In this Scenario it is passing the Text that has Created the QR – Usman Ali Oct 06 '20 at 12:26