在 SKD > 24 的可绘制文件夹中共享 png 图像

Sharing a png image in drawable folder in SKD > 24

我正在使用 This 解决方案,它在 SDK < 24 时工作正常,但应用程序在 SDK > 24 时崩溃

对于 SDK > 24

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
              String path = getExternalCacheDir() + "/shareimage.jpg";
              java.io.OutputStream out = null;
              java.io.File file = new java.io.File(path);
              try {
                  out = new java.io.FileOutputStream(file);
                  bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                  out.flush();
                  out.close();
              } catch (Exception e) {
                  e.printStackTrace();
              }
              path = file.getPath();
              Uri bmpUri = FileProvider.getUriForFile(ShareApp.this , this.getApplicationContext().getPackageName() + ".provider", file);
              Intent shareIntent = new Intent();
              shareIntent = new Intent(android.content.Intent.ACTION_SEND);
              shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
              shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
              shareIntent.setType("image/jpg");
              shareIntent.putExtra(Intent.EXTRA_TEXT, message);
              startActivity(Intent.createChooser(shareIntent, "Share with"));