Search code examples
javaandroidqr-code

How to share image with a button?


I am making QR code generator So far I made a generator button and save button. It works fine. I am trying to work on the sharing button. It takes a few days to figure out as a beginner and still I cannot make it work. At this code, if I click share, then the app closes.

/**Barcode share*/

        findViewById(R.id.share_barcode).setOnClickListener(v -> {
            Bitmap b = BitmapFactory.decodeResource(getResources(),R.id.qr_image);
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null);
            Uri imageUri =  Uri.parse(path);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(share, "Select"));
        });

I guessed the problem was path. I use savepath to save qr code image. And then maybe it conflicts with String path So I tried String savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";; It's not working. So maybe it's different problem and I don't know how to fix it. Could you show me how to fix?

MainActivity

public class MainActivity extends AppCompatActivity {

    private String inputValue;
    private String savePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + "/Camera/";
    private Bitmap bitmap;
    private QRGEncoder qrgEncoder;
    private ImageView qrImage;
    private EditText edtValue;
    private AppCompatActivity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        qrImage = findViewById(R.id.qr_image);
        edtValue = findViewById(R.id.edt_value);
        activity = this;


/**Barcode Generator*/
        findViewById(R.id.generate_barcode).setOnClickListener(view -> {
            inputValue = edtValue.getText().toString().trim();
            if (inputValue.length() > 0) {
                WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
                Display display = manager.getDefaultDisplay();
                Point point = new Point();
                display.getSize(point);
                int width = point.x;
                int height = point.y;
                int smallerDimension = width < height ? width : height;
                smallerDimension = smallerDimension * 3 / 4;

                qrgEncoder = new QRGEncoder(
                        inputValue, null,
                        QRGContents.Type.TEXT,
                        smallerDimension);
                qrgEncoder.setColorBlack(Color.BLACK);
                qrgEncoder.setColorWhite(Color.WHITE);
                try {
                    bitmap = qrgEncoder.getBitmap();
                    qrImage.setImageBitmap(bitmap);
                } catch (Exception e) {
                    e.printStackTrace();

                }
            } else {
                edtValue.setError(getResources().getString(R.string.value_required));
            }
        });
/**Barcode save*/
        findViewById(R.id.save_barcode).setOnClickListener(v -> {
            String filename = edtValue.getText().toString().trim();
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                try {
                    ContentResolver resolver = getContentResolver();
                    ContentValues contentValues = new ContentValues();
                    contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, filename + ".jpg");
                    contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "image/jpg");
                    contentValues.put(MediaStore.MediaColumns.RELATIVE_PATH, Environment.DIRECTORY_DCIM);
                    Uri imageUri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues);
                    OutputStream fos = resolver.openOutputStream(Objects.requireNonNull(imageUri));
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
                    Objects.requireNonNull(fos).close();
                    Toast toast= Toast.makeText(getApplicationContext(),
                            "Image Saved. Check your gallery.", Toast.LENGTH_SHORT);
                    toast.setGravity(Gravity.TOP|Gravity.CENTER_HORIZONTAL, 0, 0);
                    toast.show();

                    edtValue.setText(null);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            } else {
                if (ContextCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
                    try {
                        boolean save = new QRGSaver().save(savePath, filename, bitmap, QRGContents.ImageType.IMAGE_JPEG);
                        String result = save ? "Image Saved. Check your gallery." : "Image Not Saved";
                        Toast.makeText(activity, result, Toast.LENGTH_LONG).show();
                        edtValue.setText(null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ActivityCompat.requestPermissions(activity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
                }
            }
        });


        /**Barcode share*/

        findViewById(R.id.share_barcode).setOnClickListener(v -> {
            Bitmap b = BitmapFactory.decodeResource(getResources(),R.id.qr_image);
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("image/jpeg");
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            b.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            String path = MediaStore.Images.Media.insertImage(getContentResolver(), b, "Title", null);
            Uri imageUri =  Uri.parse(path);
            share.putExtra(Intent.EXTRA_STREAM, imageUri);
            startActivity(Intent.createChooser(share, "Select"));
        });



    }


}


Solution

  • I think you can solve this by:

    1. Saving the bitmap in a file.
    2. Then sharing the URI of that file in the intent.

    In the code below, I am saving the image at the app level directory, you can choose your own and the code is written in kotlin.

    Note: If you are using an app-level directory for saving the image then you must use the file provide to get the URI else it may result in FileUriExposedException

    try {
        val file = File(getExternalFilesDir(null),System.currentTimeMillis().toString() + ".png")
        file.createNewFile()
        val b = imageView.drawable.toBitmap()
        FileOutputStream(file).use { out ->
            b.compress(Bitmap.CompressFormat.PNG, 100, out)
        }
    
        val share = Intent(Intent.ACTION_SEND)
        share.type = "image/jpeg"
        val photoURI = FileProvider.getUriForFile(this, applicationContext.packageName.toString() + ".provider", file)
    
        share.putExtra(Intent.EXTRA_STREAM, photoURI)
        startActivity(Intent.createChooser(share, "Share Image"))
    
        Toast.makeText(this, "Completed!!", Toast.LENGTH_SHORT).show()
    } catch (e: IOException) {
        e.printStackTrace()
        Toast.makeText(this, e.message, Toast.LENGTH_SHORT).show()
    }
    

    In JAVA:

    
    public void shareImage(Activity activity, ImageView imageView) {
            try {
                File file = new File(activity.getExternalFilesDir(null), System.currentTimeMillis() + ".png");
                file.createNewFile();
    
                Bitmap bitmap = drawableToBitmap(imageView.getDrawable());
                FileOutputStream fOut = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.PNG, 85, fOut);
                fOut.flush();
                fOut.close();
        
                Intent share = new Intent("android.intent.action.SEND");
                share.setType("image/jpeg");
                Uri photoURI = FileProvider.getUriForFile(activity,activity.getPackageName(), file);
                share.putExtra("android.intent.extra.STREAM", photoURI);
                activity.startActivity(Intent.createChooser(share, "Share Image"));
            } catch (Exception var14) {
    
            }
        }
    
        public static Bitmap drawableToBitmap (Drawable drawable) {
            Bitmap bitmap;
    
            if (drawable instanceof BitmapDrawable) {
                BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
                if(bitmapDrawable.getBitmap() != null) {
                    return bitmapDrawable.getBitmap();
                }
            }
    
            if(drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
                bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
            } else {
                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
            }
    
            Canvas canvas = new Canvas(bitmap);
            drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            drawable.draw(canvas);
            return bitmap;
        }