title: Permission Denial: opening provider android.support.v4.content.FileProvider
body: <p>I was having some problem when trying to perform an application upgrade in Android emulator. The flow of the scenario is from an Activity, I will execute <strong>AsyncTask A</strong> which open up <strong>fragment A</strong>, then inside <strong>AsyncTask A</strong>, I will check if version upgrade is available. </p>
  
  <p>If available and user selected "Okay" from <strong>fragment A</strong>, I will proceed to <strong>AsyncTask B</strong> to open up <strong>fragment B</strong> which show a message to user saying that upgrading is in process. In <strong>AsyncTask B</strong> <em>doInBackground()</em>, I will execute the <em>install()</em> and in <em>onPostExecute()</em>, I will show successful message. </p>
  
  <p>In my AsyncTask B where I execute the install:</p>
  
  <pre><code>@Override
  protected Boolean doInBackground(Void... params) {
  boolean ret= viewmodel.installApk(mActivity);
  return ret;
  }
  </code></pre>
  
  <p>In my view model class:</p>
  
  <pre><code>public boolean installApk(Activity mActivity){
      boolean success = false;
      String fullPath = scanDirectoryForApk();
  
      System.out.println("INSTALLING APK ......................... ");
  
      Intent intent = new Intent(Intent.ACTION_VIEW);
      Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
      intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
      intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      mActivity.startActivity(intent);
      return true;
   }
  </code></pre>
  
  <p>However, when I execute the code above, no error message was shown and the version upgrade is not working as well. It basically just restart the intent and there is no upgrade at all. </p>
  
  <p>It does not prompt me for the permission to install new version as well. Any ideas?</p>
  
  <p>By the way, my android emulator is not rooted and therefore I could not use the "su" command approach.</p>
  
  <p>Thanks! </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>As suggestion by @Sagar, I changed my code above to:</p>
  
  <pre><code>Intent intent = new Intent(Intent.ACTION_VIEW);
      Uri apkURI = FileProvider.getUriForFile(mActivity, mActivity.getApplicationContext().getPackageName() + ".provider", new File(fullPath));
  
      List&lt;ResolveInfo&gt; resInfoList = mActivity.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
      for (ResolveInfo resolveInfo : resInfoList) {
          String packageName = resolveInfo.activityInfo.packageName;
          mActivity.grantUriPermission(packageName, apkURI, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
      }
  
      intent.setDataAndType(apkURI, "application/vnd.android.package-archive");
      intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      mActivity.startActivity(intent);
  </code></pre>
  
  <p>And I am getting new error message from logcat: </p>
  
  <pre><code>Error staging apk from content URI
  java.lang.SecurityException: Permission Denial: opening provider android.support.v4.content.FileProvider from ProcessRecord{3883c8 9647:com.google.android.packageinstaller/u0a20} (pid=9647, uid=10020) that is not exported from UID 10085
  </code></pre>
  
  <p>The intent error message is telling me "There was a problem parsing package".</p>