java - "file:///storage/emulated/0/screenshot.png exposed beyond app through ClipData.Item.getUri()"

标签 java android-studio

我正在尝试在我的应用程序上设置共享按钮。该按钮应该截取特定 ListView 的屏幕截图,然后允许用户通过他们想要的任何方式共享该图像。为此,我创建了三种方法:

  1. 截取屏幕截图
  2. 将其保存在我的存储空间
  3. 将其分享给用户。

为此,我编写了以下代码:

public class ViewPlayerHistoryContents extends AppCompatActivity {    

    public static File imagePath;

    public Bitmap takeScreenshot() {
        View rootView = findViewById(android.R.id.content).getRootView();
        rootView.setDrawingCacheEnabled(true);
        return rootView.getDrawingCache();
    }

    public void saveBitmap(Bitmap bitmap) {
        imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream(imagePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (FileNotFoundException e) {
            Log.e("GREC", e.getMessage(), e);
        } catch (IOException e) {
            Log.e("GREC", e.getMessage(), e);
        }
    }

    private void shareIt() {
        Uri uri = Uri.fromFile(imagePath);
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("image/*");
        String shareBody = "In Tweecher, My highest score with screen shot";
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "My Tweecher score");
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
        sharingIntent.putExtra(Intent.EXTRA_STREAM, uri);

        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_player_history);        
        shareButton = findViewById(R.id.shareButton);    
        View rootView = getWindow().getDecorView().findViewById(android.R.id.content);

        shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bitmap bitmap = takeScreenshot();
                saveBitmap(bitmap);
                shareIt();    
            }
        });        
    }
}

使用此代码,我不断收到以下错误消息:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.transfergame, PID: 18477
android.os.FileUriExposedException: file:///storage/emulated/0/screenshot.png exposed beyond app through ClipData.Item.getUri()

对我来说,这听起来像是因为我使用的是 Uri 而不是 FileProvider?

我应该如何将 FileProvider 合并到其中?

我向 Android list 文件添加了以下权限,但它没有执行任何操作:

</application>        
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如何修复此错误?

谢谢大家

最佳答案

您可以通过this

您已在 android list 中添加提供程序,并在 xml/provider.xml 中创建文件提供程序,如链接中的答案所示。

关于java - "file:///storage/emulated/0/screenshot.png exposed beyond app through ClipData.Item.getUri()",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62325588/

相关文章:

git - 为什么不在.gitignore中的Android项目中排除gradlew文件?

java - 如何使用自定义 PolicySpi

java - 使用 apache poi 将单元格内容的一部分设置为粗体?

Java VisualVM 在 OOME 上启用堆转储

java - 如何使用 Android Webview 检查链接是否离线或损坏?

android - 如何将垂直准则转换为百分比

android - Android Studio 构建错误

java - 在Android项目布局中添加Scrollview

java swing - 执行的按钮操作不起作用

android - Android Studio无法解决Gradle依赖关系