public void sharePhoto() {

    final FloatingActionButton share = (FloatingActionButton) findViewById(R.id.share);
    final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    share.setEnabled(false);
    share.setIndeterminate(true);

    Thread t =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                pushObjectToSP();

                File directory_gags =
                    new File(
                        getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
                            + File.separator
                            + "gags");
                if (!directory_gags.exists()) {
                  directory_gags.mkdirs();
                }
                final File dir_app = new File(directory_gags + File.separator + photo_id);

                FileOutputStream out = null;
                try {
                  out = new FileOutputStream(dir_app);
                  bp.getBitmap(true).compress(Bitmap.CompressFormat.PNG, 100, out);
                } catch (Exception e) {
                  e.printStackTrace();
                } finally {
                  try {
                    if (out != null) {
                      out.close();
                    }
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                }

                runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        share.setEnabled(true);
                        share.setIndeterminate(false);
                      }
                    });

                Uri uri = Uri.fromFile(dir_app);
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_SEND);
                intent.setType("image/*");

                Boolean boo = prefs.getBoolean("useTitleAsMessage", false);

                intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
                if (boo) {
                  intent.putExtra(android.content.Intent.EXTRA_TEXT, gagTitle);
                } else {
                  intent.putExtra(android.content.Intent.EXTRA_TEXT, "");
                }
                intent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(intent, "Share Gag to..."));
              }
            });
    t.start();
  }
  public void savePhoto() {

    final FloatingActionButton save = (FloatingActionButton) findViewById(R.id.save);
    save.setEnabled(false);
    save.setIndeterminate(true);

    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
    String file_path = prefs.getString("path", null);

    String format = prefs.getString("image_format", "0");

    class FORMAT {
      public String ext;
      public Bitmap.CompressFormat bitmap_format;
      public Integer q;

      public void setString(String x) {
        this.ext = x;
      }

      public void setBitmapFormat(Bitmap.CompressFormat x) {
        this.bitmap_format = x;
      }

      public void setQuality(Integer x) {
        this.q = x;
      }
    }

    final FORMAT f = new FORMAT();

    if (format.equals("0")) {
      f.setString(".png");
      f.setBitmapFormat(Bitmap.CompressFormat.PNG);
      f.setQuality(100);
    } else {
      f.setString(".jpeg");
      f.setBitmapFormat(Bitmap.CompressFormat.JPEG);
      f.setQuality(80);
    }

    assert file_path != null;
    final File dir = new File(file_path);
    if (!dir.exists()) {
      dir.mkdirs();
    }

    Thread normal_save =
        new Thread(
            new Runnable() {
              @Override
              public void run() {
                pushObjectToSP();

                String modifiedTitle = gagTitle.replaceAll(" ", "-");
                File file = new File(dir, modifiedTitle + f.ext);

                FileOutputStream outo = null;
                try {
                  outo = new FileOutputStream(file);
                  bp.getBitmap(true).compress(f.bitmap_format, f.q, outo);
                } catch (Exception e) {
                  e.printStackTrace();
                } finally {
                  try {
                    if (outo != null) {
                      outo.close();
                    }
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                }

                File directory_gags =
                    new File(
                        getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
                            + File.separator
                            + "gags");
                if (!directory_gags.exists()) {
                  directory_gags.mkdirs();
                }
                File dir_app = new File(directory_gags + File.separator + photo_id);
                FileOutputStream outi = null;

                try {
                  outi = new FileOutputStream(dir_app);
                  bp.getBitmap(true).compress(f.bitmap_format, f.q, outi);
                } catch (Exception e) {
                  e.printStackTrace();
                } finally {
                  try {
                    if (outi != null) {
                      outi.close();
                    }
                  } catch (IOException e) {
                    e.printStackTrace();
                  }
                }

                Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                intent.setData(Uri.fromFile(file));
                sendBroadcast(intent);

                runOnUiThread(
                    new Runnable() {
                      @Override
                      public void run() {
                        save.setIndeterminate(false);
                        Toast.makeText(
                                getApplicationContext(),
                                "Gag saved successfully",
                                Toast.LENGTH_LONG)
                            .show();
                      }
                    });
              }
            });

    normal_save.start();
  }