仅在 Nougat 设备中通过 intent 共享后图像不可见

Image is not visible after sharing via intent in Nougat devices only

伙计们,我一直面临有关在 nougat 设备中共享图像的问题,但不知道为什么在选择共享应用程序(ex-facebook)后,我的图像不可见.下面是截图

  share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.getDefault()).format(new Date());
            Bitmap bitmap = viewToBitmap(canvas);
            shareBitmap(bitmap, timeStamp + ".png");
        }
    });


    private void shareBitmap(Bitmap bitmap, String fileName) {
    try {

        File file = new File(getCacheDir(), fileName + ".png");
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
        intent.setType("image/png");
        startActivity(Intent.createChooser(intent, "Share Image"));
    } catch (Exception e) {
        Log.e("exception :>>", e.toString());
        e.printStackTrace();
    }
}

通过使用 FileProvider:

AndroidManifest.xml

 <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.provider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
  <external-path name="external_files" path="."/>
</paths>

save_share.java

share.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                    Locale.getDefault()).format(new Date());
            Bitmap bitmap = viewToBitmap(canvas);
            shareBitmap(bitmap, timeStamp + ".png");
        }
    });


    private void shareBitmap(Bitmap bitmap, String fileName) {
    try {

        File file = new File(getCacheDir(), fileName + ".png");
        FileOutputStream fOut = new FileOutputStream(file);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        fOut.flush();
        fOut.close();
        file.setReadable(true, false);
    Uri photoURI = FileProvider.getUriForFile(save_share.this, BuildConfig.APPLICATION_ID + ".provider", file);
        final Intent intent = new Intent(android.content.Intent.ACTION_SEND);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.putExtra(Intent.EXTRA_STREAM, photoURI);
        intent.setType("image/png");
        startActivity(Intent.createChooser(intent, "Share Image"));
    } catch (Exception e) {
        Log.e("exception :>>", e.toString());
        e.printStackTrace();
    }
}

这只会发生在 Nougat 设备上。请帮我找到解决办法

This is only happen with Nougat devices.

您已将图像存储在您应用的内部存储部分,其他应用无法访问该部分。

您已尝试通过调用 setReadable(true, false) 来解决这个问题。这是不安全的,因为它允许任何应用程序读取此文件。并且,从 Android 7.0 开始,IIRC,setReadable() 不能再使用了。

使用FileProvider与其他应用分享内容。