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

时间:2018-10-11 06:57:54

标签: android share

我正在使用This解决方案,它可以与SDK <24正常工作,但是应用程序在SDK> 24中崩溃了

1 个答案:

答案 0 :(得分:1)

对于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"));