Skip to content
Merged
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
53 changes: 48 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,19 @@ const fileDescriptor = { name: 'sample', parentFolder: 'MyAppFolder', mimeType:
const contentURI = await RNFS.MediaStore.createMediaFile(fileDescriptor, RNFS.MediaStore.MEDIA_IMAGE)
```

### `updateMediaFile`

* Updates the media file in the MediaStore

```ts
// updateMediaFile(uri: string, fileDescriptor: FileDescriptor, mediatype: MediaCollections): Promise<string>

const contentURI = 'content://media/external/images/media/123'
const fileDescriptor = { name: 'sample-updated-filename', parentFolder: 'MyAppFolder', mimeType: 'image/png' }

const contentURI = await RNFS.MediaStore.updateMediaFile(contentURI, fileDescriptor, RNFS.MediaStore.MEDIA_IMAGE)
```

### `writeToMediaFile`

* Writes data to a media file in the MediaStore with the given `mimeType`.
Expand All @@ -337,14 +350,27 @@ const fileDescriptor = { name: 'sample', parentFolder: 'MyAppFolder', mimeType:
const contentURI = await RNFS.MediaStore.copyToMediaStore(fileDescriptor, RNFS.MediaStore.MEDIA_IMAGE, '/path/to/image/imageToCopy.png')
```

### `existsInMediaStore`
### `queryMediaStore`

* Checks if the media file at `uri` exists in the MediaStore.
* Queries the MediaStore for media files with the given `searchOptions`.

```ts
// existsInMediaStore(uri: string): Promise<boolean>

await RNFS.MediaStore.existsInMediaStore('content://media/external/images/media/123')
// queryMediaStore(searchOptions: MediaStoreSearchOptions): Promise<MediaStoreQueryResult>

await RNFS.MediaStore.queryMediaStore({
uri: 'content://media/external/images/media/123',
fileName: ''
relativePath: ''
mediaType: RNFS.MediaStore.MEDIA_IMAGE;
})

// or
await RNFS.MediaStore.queryMediaStore({
uri: '',
fileName: 'image.png'
relativePath: 'MyAppFolder'
mediaType: RNFS.MediaStore.MEDIA_IMAGE;
})
```

### `deleteFromMediaStore`
Expand All @@ -366,6 +392,23 @@ type FileDescriptor = {
};
```

## MediaStoreSearchOptions
```ts
type MediaStoreSearchOptions = {
uri: string;
fileName: string;
relativePath: string;
mediaType: MediaCollections
};
```

## MediaStoreQueryResult
```ts
type MediaStoreQueryResult = {
contentUri: string;
};
```

## MediaStore Collections
* `MediaStore.MEDIA_AUDIO` - Audio media collection
* `MediaStore.MEDIA_IMAGE` - Image media collection
Expand Down
46 changes: 46 additions & 0 deletions android/src/main/java/com/rnfs2/RNFSMediaStoreManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ public void createMediaFile(ReadableMap filedata, String mediaType, Promise prom
}
}

@ReactMethod void updateMediaFile(String fileUri, ReadableMap filedata, String mediaType, Promise promise) {
if (!(filedata.hasKey("name") && filedata.hasKey("parentFolder") && filedata.hasKey("mimeType"))) {
promise.reject("RNFS2.updateMediaFile", "Invalid filedata: " + filedata.toString());
return;
}

if (mediaType == null) {
promise.reject("RNFS2.updateMediaFile", "Invalid mediatype");
return;
}

FileDescription file = new FileDescription(filedata.getString("name"), filedata.getString("mimeType"), filedata.getString("parentFolder"));
Uri fileuri = Uri.parse(fileUri);
boolean res = updateExistingMediaFile(fileuri, file, MediaType.valueOf(mediaType), promise, reactContext);
if (res) {
promise.resolve("Success");
}
}

@ReactMethod
public void writeToMediaFile(String fileUri, String path, boolean transformFile, Promise promise) {
boolean res = writeToMediaFile(Uri.parse(fileUri), path, transformFile, promise, reactContext);
Expand Down Expand Up @@ -249,6 +268,33 @@ private Uri createNewMediaFile(FileDescription file, MediaType mediaType, Promis
return null;
}

private boolean updateExistingMediaFile(Uri fileUri, FileDescription file, MediaType mediaType, Promise promise, ReactApplicationContext ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
Context appCtx = ctx.getApplicationContext();
ContentResolver resolver = appCtx.getContentResolver();

ContentValues fileDetails = new ContentValues();
String relativePath = getRelativePath(mediaType, ctx);
String mimeType = file.mimeType;

fileDetails.put(MediaStore.MediaColumns.DATE_MODIFIED, System.currentTimeMillis() / 1000);
fileDetails.put(MediaStore.MediaColumns.MIME_TYPE, mimeType);
fileDetails.put(MediaStore.MediaColumns.DISPLAY_NAME, file.name);
fileDetails.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath + '/' + file.parentFolder);

int rowsUpdated = resolver.update(fileUri, fileDetails, null, null);
return rowsUpdated > 0;
} catch (Exception e) {
promise.reject("RNFS2.updateExistingMediaFile", "Error updating file: " + e.getMessage());
return false;
}
} else {
promise.reject("RNFS2.updateExistingMediaFile", "Android version not supported");
return false;
}
}

private boolean writeToMediaFile(Uri fileUri, String filePath, boolean transformFile, Promise promise, ReactApplicationContext ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
Expand Down
10 changes: 10 additions & 0 deletions example/App/example3.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ const Example = () => {

runStatus = `${runStatus}\n- "Media File" stat: ${JSON.stringify(mediaStat)}`;
setResult(runStatus);

await RNFS.MediaStore.updateMediaFile(
contentURI,
{
name: `pewpewDummyImageEdited.png`,
parentFolder: 'RNFSExample3Folder',
mimeType: 'image/png',
},
RNFS.MediaStore.MEDIA_IMAGE,
);
} catch (err) {
setResult(`${runStatus}\n- Error Running Example`);
console.error(err);
Expand Down
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@
return RNFSMediaStoreManager.createMediaFile(fileDescriptor, mediatype);
},

updateMediaFile(uri: string, fileDescriptor: FileDescriptor, mediatype: MediaCollections): Promise<string> {
return RNFSMediaStoreManager.updateMediaFile(uri, fileDescriptor, mediatype);
},

writeToMediaFile(uri: string, path: string): Promise<void> {
return RNFSMediaStoreManager.writeToMediaFile(uri, normalizeFilePath(path), false);
},
Expand All @@ -156,15 +160,15 @@

export default {
mkdir(filepath: string, options: MkdirOptions = {}): Promise<undefined> {
return RNFSManager.mkdir(normalizeFilePath(filepath), options).then(() => void 0);

Check warning on line 163 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node 18

Expected 'undefined' and instead saw 'void'
},

moveFile(filepath: string, destPath: string, options: FileOptions = {}): Promise<undefined> {
return RNFSManager.moveFile(normalizeFilePath(filepath), normalizeFilePath(destPath), options).then(() => void 0);

Check warning on line 167 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node 18

Expected 'undefined' and instead saw 'void'
},

copyFile(filepath: string, destPath: string, options: FileOptions = {}): Promise<undefined> {
return RNFSManager.copyFile(normalizeFilePath(filepath), normalizeFilePath(destPath), options).then(() => void 0);

Check warning on line 171 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node 18

Expected 'undefined' and instead saw 'void'
},

getFSInfo(): Promise<FSInfoResult> {
Expand All @@ -176,7 +180,7 @@
},

unlink(filepath: string): Promise<void> {
return RNFSManager.unlink(normalizeFilePath(filepath)).then(() => void 0);

Check warning on line 183 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node 18

Expected 'undefined' and instead saw 'void'
},

exists(filepath: string): Promise<boolean> {
Expand Down Expand Up @@ -279,7 +283,7 @@
position = -1;
}

return RNFSManager.write(normalizeFilePath(filepath), b64, position).then(() => void 0);

Check warning on line 286 in src/index.ts

View workflow job for this annotation

GitHub Actions / Node 18

Expected 'undefined' and instead saw 'void'
},

downloadFile(options: DownloadFileOptions): DownloadFileResult {
Expand Down
Loading