title: How do I save my imageView image into Gallery (Android Development)
body: <p>I am trying to create an onClick event to save an imageview into the phone Gallery by the click of a Button, below is my code. it does not save into the Gallery, can anyone help me figure out why?</p>
  
  <pre><code>    sharebtn.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View b) {
              // TODO Auto-generated method stub
              //attempt to save the image
  
              b = findViewById(R.id.imageView);
              b.setDrawingCacheEnabled(true);
                  Bitmap bitmap = b.getDrawingCache();
                  //File file = new File("/DCIM/Camera/image.jpg");
                  File root = Environment.getExternalStorageDirectory();
                  File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
                  try 
                  {
                      cachePath.createNewFile();
                      FileOutputStream ostream = new FileOutputStream(cachePath);
                      bitmap.compress(CompressFormat.JPEG, 100, ostream);
                      ostream.close();
                  } 
                  catch (Exception e) 
                  {
                      e.printStackTrace();
                  }
  
          }
      });
  </code></pre>