Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,26 @@ private void persistToDisk(@NonNull Context context,
}

public Uri createForExternal(@NonNull Context context, @NonNull String mimeType) throws IOException, IllegalStateException, NullPointerException {
File target = new File(getExternalDir(context), System.currentTimeMillis() + "." + getExtensionFromMimeType(mimeType));
return FileProviderUtil.getUriFor(context, target);
String filename = System.currentTimeMillis() + "." + getExtensionFromMimeType(mimeType);

// Try external cache first
try {
File externalDir = getExternalDir(context);
File target = new File(externalDir, filename);
return FileProviderUtil.getUriFor(context, target);
} catch (IllegalArgumentException e) {
// FileProvider doesn't support the external cache path (e.g., on removable SD card).
// Note: getExternalDir() already falls back to internal cache when external cache is null,
// but when external cache exists on a removable SD card, FileProvider may reject it.
// In that case, we explicitly use internal cache which FileProvider always supports.
Log.w(TAG, "FileProvider doesn't support external cache path, falling back to internal cache", e);
File internalDir = context.getCacheDir();
if (internalDir == null) {
throw new IOException("no cache directory available");
}
File target = new File(internalDir, filename);
return FileProviderUtil.getUriFor(context, target);
}
}

public boolean delete(@NonNull Context context, @NonNull Uri uri) {
Expand Down
1 change: 1 addition & 0 deletions src/main/res/xml/file_provider_paths.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<paths>
<cache-path name="cache" path="." />
<external-cache-path name="external_cache" path="." />
<external-files-path name="external_files" path="." />

Expand Down
Loading