You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CacheDir截取截图时触发IllegalArgumentException异常求助

Fixing the FileProvider "Failed to find configured root" Error

Hey there, let's break down why you're hitting this java.lang.IllegalArgumentException and fix it up!

The Root Cause

Your screenshot is being saved directly to your app's main cache directory (/data/data/com.ct.app/cache/screenShot.png), but your FileProvider configuration in file_provide.xml only maps a subfolder of the cache directory (/images/). Since the file isn't in that mapped subfolder, FileProvider can't locate it.

Solution 1: Save the Screenshot to the Mapped Subfolder

Update your file creation code to save the image inside the images subdirectory of your cache folder. This matches your existing FileProvider config:

// Create the images subfolder if it doesn't exist
File cacheImagesDir = new File(this.getCacheDir(), "images");
if (!cacheImagesDir.exists()) {
    cacheImagesDir.mkdirs();
}
// Save the screenshot inside this subfolder
File file = new File(cacheImagesDir, "screenShot.png");

Solution 2: Update FileProvider to Map the Entire Cache Directory

If you prefer to keep saving files directly in the root cache folder, modify your file_provide.xml to map the full cache directory instead of just the /images/ subfolder:

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <!-- Map the entire cache directory -->
    <cache-path name="cache" path="." />
    <files-path name="files" path="/images/" />
    <external-files-path name="external_files" path="."/>
    <!--<external-cache-path name="external_files" path="." />-->
</paths>

Bonus: Fix a Minor Intent Issue

I noticed you're setting the Intent's setType twice: first to "text/plain" then to "image/*". When sharing both an image and text, you should set the type to "image/*" once to ensure the system handles the mixed content properly. Here's the corrected Intent snippet:

Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/*"); // Set type once to image/*
share.putExtra(android.content.Intent.EXTRA_TEXT, "Download Android App");
share.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(this,"com.ct.app.provider",file));
share.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(Intent.createChooser(share, "Share Image"),20);

Either of the first two solutions will resolve the "Failed to find configured root" error. Give them a try and let me know if you run into any other snags!

内容的提问来源于stack exchange,提问作者Alex

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:40:42