How can I make a share button for QR Code generator app?

  android, java, qr-code, share

I am making QR code generator app.
I am a beginner that I find the open-source and edit it little by little.
So far I made a save and generator button.

I want to put a share button. I would like to use the below code, but there are errors. Could you show me how to use this code on my MainActivity, please?

code is from here

//Share button
        findViewById(R.id.share_barcode).setOnClickListener(v -> {
            public void OnClickShare (View view){
                Bitmap bitmap = getBitmapFromView(qr_image);
                try {
                    File file = new File(this.getExternalCacheDir(), "logicchip.png");
                    FileOutputStream fOut = new FileOutputStream(file);
                    bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
                    fOut.flush();
                    fOut.close();
                    file.setReadable(true, false);
                    final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
                    intent.setType("image/png");
                    startActivity(Intent.createChooser(intent, "Share image via"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


        });

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/edt_value"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/enter_text"
        android:importantForAutofill="no"
        android:inputType="text" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal">

        <Button
            android:id="@+id/generate_barcode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/generate" />

        <Button
            android:id="@+id/save_barcode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/save" />

        <Button
            android:id="@+id/share_barcode"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/share" />

    </LinearLayout>

    <ImageView
        android:id="@+id/qr_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/app_name" />
</LinearLayout>

Source: Android Questions

LEAVE A COMMENT