expo-sdcard-detector
Version:
Expo plugin to detect SD card storage paths on Android
89 lines (61 loc) • 2.75 kB
Markdown
and native module to detect SD card storage paths on Android devices. This module helps you access external storage paths, particularly useful for applications that need to interact with SD cards or external storage.
- 🔍 Detect all external storage paths on Android devices
- 📱 Compatible with Expo and React Native projects
- 🔧 Easy to integrate with existing Expo applications
- 📦 TypeScript support included
- 🛡️ Safe to use on iOS (returns empty array)
## Requirements
- Expo SDK 49 or higher
- React Native 0.72 or higher
- Android device with external storage support
## Installation
```bash
npm install expo-sdcard-detector
# or
yarn add expo-sdcard-detector
```
## Configuration
Add the plugin to your `app.config.js` or `app.config.ts`:
```js
module.exports = {
name: "my-app",
plugins: ["expo-sdcard-detector"]
};
```
```ts
import { getExternalStoragePaths } from 'expo-sdcard-detector/native';
// Get all external storage paths
const paths = await getExternalStoragePaths();
console.log(paths);
// Example output on Android: ['/storage/emulated/0', '/storage/sdcard1']
// Example output on iOS: []
```
```ts
import { getExternalStoragePaths } from 'expo-sdcard-detector/native';
import * as FileSystem from 'expo-file-system';
const [sdPath] = await getExternalStoragePaths(); // e.g. "/storage/sdcard1/Android/data/…/files"
if (sdPath) {
const fileUri = `file://${sdPath}/myFolder/hello.txt`;
await FileSystem.makeDirectoryAsync(`file://${sdPath}/myFolder`, { intermediates: true });
await FileSystem.writeAsStringAsync(fileUri, 'Hello SD-card!');
const info = await FileSystem.getInfoAsync(fileUri);
console.log(info);
}
```
- The function only returns paths for removable volumes; the internal storage path (/storage/emulated/0/…) is already available via FileSystem.documentDirectory.
- iOS will always return an empty array ([]).
- If you need to touch folders outside your app’s sandbox (e.g. the root of the SD-card or the user’s DCIM directory) you’ll have to use the Storage Access Framework (expo-storage-access-framework) or request the MANAGE_EXTERNAL_STORAGE permission on Android 11+, because Expo FileSystem alone may not bypass scoped-storage rules.
## API Reference
### `getExternalStoragePaths()`
Returns a Promise that resolves to an array of strings containing the paths to external storage devices.
- **Android**: Returns an array of external storage paths (including SD card paths)
- **iOS**: Returns an empty array `[]`
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## License
MIT
An Expo config plugin