How to share QR generated image in Android Studio Java

153 Views Asked by At

Im trying to share my qr generated image but when I try it, the QR code image is not appearing on the selected share method.output

Im tryin to ceate a function where, when a user register it will generate a QR code and pop it up using a dialog box, then when the user click the Okay button on the dialog box it should share the QR code via the selected method of the user. I tried following a tutorial on YoutTube by Android Tutorials: https://www.youtube.com/watch?v=eSi28xqGjbE, the share function is working but it doesnt show the generated QR code in the selected method for sharing the image.

public void showQR() {
        Dialog dialog = new Dialog(this);
        dialog.setContentView(R.layout.cutome_dialog_layout);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            dialog.getWindow().setBackgroundDrawable(getDrawable(R.drawable.custom_dialog_background));
        }
        dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.setCancelable(false); //Optional
        dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; //Setting the animations to dialog

        Button Okay = dialog.findViewById(R.id.btn_okay);
        Button Cancel = dialog.findViewById(R.id.btn_cancel);
        ImageView qr_image = dialog.findViewById(R.id.qrcode);

        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();

        try {
            BitMatrix bitMatrix = multiFormatWriter.encode(studentNumber.getText().toString()+ " ," +fullName.getText().toString(), BarcodeFormat.QR_CODE, 300, 300);

            BarcodeEncoder barcodeEncoder = new BarcodeEncoder();
            Bitmap bitmap = barcodeEncoder.createBitmap(bitMatrix);

            qr_image.setImageBitmap(bitmap);

        }
        catch (WriterException e) {
            throw new RuntimeException(e);
        }


        Okay.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                try {
                    BitmapDrawable bitmapDrawable = (BitmapDrawable) qr_image.getDrawable();
                    Bitmap bitmap = bitmapDrawable.getBitmap();
                    shareImageAndText(bitmap);
                } catch (Exception e) {
                    Toast.makeText(RegisterStudent.this, "share image problem", Toast.LENGTH_SHORT).show();
                }
                dialog.dismiss();
            }
        });

        Cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Toast.makeText(RegisterStudent.this, "Cancel", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });

        dialog.show();
    }

    private void shareImageAndText(Bitmap bitmap) {
        Uri uri = getImageToShare(bitmap);

        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.putExtra(Intent.EXTRA_STREAM, uri);
        intent.putExtra(Intent.EXTRA_TEXT, "Please dont share your QR Code to anyone");
        intent.putExtra(Intent.EXTRA_SUBJECT, "QR Code");
        intent.setType("images/*");
        startActivity(Intent.createChooser(intent, "Share via"));
    }

    private Uri getImageToShare(Bitmap bitmap) {
        File folder = new File(getCacheDir(), "images");
        Uri uri = null;
        try {
        folder.mkdirs();
        File file = new File(folder, "image.jpg");
        FileOutputStream fileOutputStream = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fileOutputStream);
        fileOutputStream.flush();
        fileOutputStream.close();

        uri = FileProvider.getUriForFile(this, "com.example.myapplication", file);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return uri;
    }

Please help me on how can i share my QR-generated code.

Expected output. expected output

1

There are 1 best solutions below

3
CommonsWare On

Replace:

intent.setType("images/*");

with:

intent.setType("image/jpeg");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

You are the one supplying the content, so you need to be the one indicating the MIME type of that content. Also, you need to give permission to the other app to access that content.