title: android.os.FileUriExposedException: at android.os.StrictMode.onFileUriExposed
body: <p>I'm following <a href="https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en" rel="nofollow noreferrer">this tutorial</a> for using FileProvider instead of <code>Uri.FromFile()</code> But I'm not sure how to use FileProvider in my service.</p>
  
  <p><code>FileProvider.getUriForFile()</code> expects a context for the first argument but because  I'm using this in my service, I don't have an activity context.</p>
  
  <p>What am I supposed to do?</p>
  
  <p>I need to change this:</p>
  
  <pre><code>Uri swatchImageUri = Uri.fromFile(favoritesImageFile);
  </code></pre>
  
  <p>To what they suggestion above.</p>
  
  <p>Here's the current exception I've been getting without using FileProvider: </p>
  
  <pre><code>2019-07-23 23:04:42.748 11034-11034/com.company.projname E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.company.projname, PID: 11034
      android.os.FileUriExposedException: file:///storage/emulated/0/Android/data/com.company.projname/cache/colors.png exposed beyond app through ClipData.Item.getUri()
          at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
          at android.net.Uri.checkFileUriExposed(Uri.java:2371)
          at android.content.ClipData.prepareToLeaveProcess(ClipData.java:963)
          at android.content.Intent.prepareToLeaveProcess(Intent.java:10216)
          at android.content.Intent.prepareToLeaveProcess(Intent.java:10222)
          at android.content.Intent.prepareToLeaveProcess(Intent.java:10201)
          at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667)
          at android.app.Activity.startActivityForResult(Activity.java:4586)
  </code></pre>
  
  <p>Code snippet: </p>
  
  <pre><code>public void shareColorSwatch(SwatchViewModel model){
  
          try {
              String swatchName = model.First.get().Name.get().replaceAll("\\s","").replaceAll("'", "") + "_" +
                      model.Second.get().Name.get().replaceAll("\\s","").replaceAll("'", "") + "_" +
                      model.Third.get().Name.get().replaceAll("\\s","").replaceAll("'", "");
  
  
              Display display = activity.getWindowManager().getDefaultDisplay();
              int screenWidth =  display.getWidth();
              int screenHeight = display.getHeight();
  
              Bitmap swatchBitmap = createColorSwatchBitmap(model, screenWidth, screenHeight);
  
              String fileName = String.format("swatch_%s.png", swatchName);
              File swatchImageFile = getImageFile(activity,fileName, swatchBitmap, true);
              Uri swatchImageUri = FileProvider.getUriForFile(//,
                      BuildConfig.APPLICATION_ID + ".provider",
                      swatchImageFile);
              //Uri swatchImageUri = Uri.fromFile(swatchImageFile);
              shareImage(activity,swatchName,swatchImageUri);
          }catch (Exception e)
          {
              e.printStackTrace();
          }
  
      }
  </code></pre>