如何在Android Studio Java中共享QR生成的图像

问题描述 投票:0回答:1

我正在尝试分享我的二维码生成的图像,但是当我尝试时,二维码图像没有出现在所选的共享方法上。output

我尝试创建一个功能,当用户注册时,它将生成一个二维码并使用对话框将其弹出,然后当用户单击对话框上的“确定”按钮时,它应该通过所选方法共享二维码用户的。我尝试按照 Android 教程上的 YouTubeTube 教程进行操作:https://www.youtube.com/watch?v=eSi28xqGjbE,共享功能可以正常工作,但在所选的共享图像方法中没有显示生成的二维码.

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;
    }

请帮助我如何分享我的二维码生成的代码。

预期产出。 expected output

java android qr-code zxing
1个回答
0
投票

更换:

intent.setType("images/*");

与:

intent.setType("image/jpeg");

You 是提供内容的人,因此 you 需要指示该内容的 MIME 类型。

© www.soinside.com 2019 - 2024. All rights reserved.