title: How to save an image in a subdirectory on android Q whilst remaining backwards compatible
body: <p>I'm creating a simple image editor app and therefore need to load and save image files. I'd like the saved files to appear in the gallery in a separate album. From Android API 28 to 29, there have been drastic changes to what extent an app is able to access storage. I'm able to do what I want in Android Q (API 29) but that way is not backwards compatible. </p>
  
  <p>When I want to achieve the same result in lower API versions, I have so far only found way's, which require the use of deprecated code (as of API 29). </p>
  
  <p>These include:</p>
  
  <ul>
  <li>the use of the <code>MediaStore.Images.Media.DATA</code> column</li>
  <li>getting the file path to the external storage via <code>Environment.getExternalStoragePublicDirectory(...)</code></li>
  <li>inserting the image directly via <code>MediaStore.Images.Media.insertImage(...)</code></li>
  </ul>
  
  <p>My question is: is it possible to implement it in such a way, so it's backwards compatible, but doesn't require deprecated code? If not, is it okay to use deprecated code in this situation or will these methods soon be deleted from the sdk? In any case it feels very bad to use deprecated methods so I'd rather not :)</p>
  
  <p>This is the way I found which works with API 29:</p>
  
  <pre class="lang-java prettyprint-override"><code>ContentValues values = new ContentValues();
  String filename = System.currentTimeMillis() + ".jpg";
  
  values.put(MediaStore.Images.Media.TITLE, filename);
  values.put(MediaStore.Images.Media.DISPLAY_NAME, filename);
  values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
  values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
  values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
  values.put(MediaStore.Images.Media.RELATIVE_PATH, "PATH/TO/ALBUM");
  
  getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);
  </code></pre>
  
  <p>I then use the URI returned by the insert method to save the bitmap. The Problem is that the field RELATIVE_PATH was introduced in API 29 so when I run the code on a lower version, the image is put into the "Pictures" folder and not the "PATH/TO/ALBUM" folder.</p>