title: BitmapFactory can't decode a Bitmap from Uri after photos taken on Android Nougat
body: <p>I tried to take a photo and then to use the photo. Here is what I did.</p>
  
  <p>My device was <strong>Nexus 6P (Android 7.1.1)</strong>.</p>
  
  <p>First, I created a <code>Uri</code>:</p>
  
  <pre><code>Uri mPicPath = UriUtil.fromFile(this, UriUtil.createTmpFileForPic());
  //Uri mPicPath = UriUtil.fromFile(this, UriUtil.createFileForPic());
  </code></pre>
  
  <p>And then, I started <code>Intent</code>: </p>
  
  <pre><code>Intent intent = ActivityUtils.getTakePicIntent(mPicPath);
  if (intent.resolveActivity(getPackageManager()) != null) {
      startActivityForResult(intent, RequestCode.TAKE_PIC);
  }
  </code></pre>
  
  <p>At last, I handled this <code>Uri</code> on <code>onActivityResult</code>:</p>
  
  <pre><code>if (requestCode == RequestCode.TAKE_PIC) {
      if (resultCode == RESULT_OK &amp;&amp; mPicPath != null) {
          Bitmap requireBitmap = BitmapFactory.decodeFile(mPicPath.getPath());
          //path is like this: /Download/Android/data/{@applicationId}/files/Pictures/JPEG_20170216_173121268719051242.jpg
          requireBitmap.recycle();//Here NPE was thrown.
      }
  }
  </code></pre>
  
  <p>At the meantime, Here are <code>UriUtil</code>: </p>
  
  <pre><code>public class UriUtil {
  
      public static File createFileForPic() throws IOException {
          String fileName = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.getDefault()).format(new Date()) + ".jpg";
          File storageDic = SPApplication.getInstance().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
          return new File(storageDic, fileName);
      }
  
      public static File createTmpFileForPic() throws IOException {
          String fileName = "JPEG_" + new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.getDefault()).format(new Date());
          File storageDic = SPApplication.getInstance().getExternalFilesDir(Environment.DIRECTORY_PICTURES);
          return File.createTempFile(fileName, ".jpg", storageDic);
      }
  
      public static Uri fromFile(@NonNull Context context, @NonNull File file) {
          if (context == null || file == null) {
              throw new RuntimeException("context or file can't be null");
          }
          if (ActivityUtils.requireSDKInt(Build.VERSION_CODES.N)) {
              return FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".file_provider", file);
          } else {
              return Uri.fromFile(file);
          }
      }
  }
  </code></pre>
  
  <p>and <code>getTakePicIntent(Uri)</code>:</p>
  
  <pre><code>public static Intent getTakePicIntent(Uri mPicPath) {
      Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(MediaStore.EXTRA_OUTPUT, mPicPath);
      if (!ActivityUtils.requireSDKInt(Build.VERSION_CODES.KITKAT_WATCH)) {//in pre-KitKat devices, manually grant uri permission.
          List&lt;ResolveInfo&gt; resInfoList = SPApplication.getInstance().getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
          for (ResolveInfo resolveInfo : resInfoList) {
              String packageName = resolveInfo.activityInfo.packageName;
              SPApplication.getInstance().grantUriPermission(packageName, mPicPath, Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
          }
      } else {
          intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
          intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
      }
      return intent;
  }
  </code></pre>
  
  <p>and <code>requireSDKInt</code>: </p>
  
  <pre><code>public static boolean requireSDKInt(int sdkInt) {
      return Build.VERSION.SDK_INT &gt;= sdkInt;
  }
  </code></pre>
  
  <p>Everything worked on different Android API except on <strong>Android Nougat(7.x.x)</strong>. Even 'FileProvider' was provided, 'requireBitmap' always be returned as 'null'. </p>
  
  <p>After logs read, <code>FileNotFoundException</code> was thrown from <code>BitmapFactory</code>. It was like: </p>
  
  <pre><code>BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /Download/Android/data/{@applicationId}/files/Pictures/JPEG_20170216_1744551601425984925.jpg (No such file or directory)
  </code></pre>
  
  <p>It seems all is clear, but I still can't understand that. </p>
  
  <p>How could it be? Clearly I created a <code>File</code>! How could I solve that? Any ideas?</p>