readAsArrayBuffer not resolving

Hi folks,

im trying to read a video file from the storage and i saw a approach which uses the readAsArrayBuffer method.

Its exaclty what im looking for but the promise never resolves. I also saw an github issue where someone had the same problem and they suggested to change the order of script loading.

Im using ionic with angular and i dont know where i make such an change. Did someone had the same problem and knows what to do ? Another thing is that the issue is from 2017 and i would suppose that this error should exist in the current version of ionic.

Here is my code maybe im implementing it wrong.

Im trying to generate a blob from a local video file recorded with the media capture plugin.

async selectNewVideo() {

		try {
			this.selectedMedia = undefined;
			const video = await this.mediaCaptureService.getCameraVideo();
			
			let localVideoPath = video.fullPath;
			let directoryPath =
				localVideoPath.substr(0, localVideoPath.lastIndexOf('/'));
			let fileName = localVideoPath.substr(localVideoPath.lastIndexOf('/') + 1);
			const directoryEntry = await this.file.resolveDirectoryUrl(directoryPath);
			this.file.readAsArrayBuffer(directoryPath, fileName).then((buffer) => {
				console.log(buffer)
			})		
		} catch (error) {
			console.log(error);
		}
	}

Best regards

lcatania

1 Like

Did you figure this out? Ran into this issue as well.

While you wait for better answers, I would suggest ditching the cordova File plugin (if you’re using it, like OP was) in favor of Capacitor’s Filesystem.

EDIT: This didn’t work. I ended up switching to capacitor. I was encountering an error that no longer occurred when I made the below change, but i couldn’t get the array buffer data - it was empty

original post:
To anyone else that comes here - i was able to resolve the issue by awaiting the result vs using a promise:

this didn’t work:

this.file.readAsArrayBuffer(directoryPath, fileName).then((buffer) => {
				console.log(buffer)
			})

this did:

const buffer = await this.file.readAsArrayBuffer(directoryPath, fileName);

FYI I’m in ionic 5 and am currently using this version:
@ionic-native/file”: “^5.23.0”,

Can you provide any more context for your claim? Given only what you’ve posted here, there should be no discernable difference between await and an explicit Promise. However, if you are talking about something like this:

this.file.readAsArrayBuffer(directoryPath, fileName).then((buffer) => {
  console.log(buffer)
});
proceedToDoSomethingDependingOn(buffer);

…versus…

const buffer = await this.file.readAsArrayBuffer(directoryPath, fileName);
proceedToDoSomethingDependingOn(buffer);

…there could be a difference, but I would argue that is just exposing a bug in app code where buffer is being improperly relied upon.

So, to summarize (and I’m always happy to be corrected on this or anything else): I do not believe that async/await lets one do anything that cannot be done with a properly scoped spawnPromise().then(() => {...}) construction. Reasonable people can disagree about which option they prefer stylistically, but I don’t think there’s a case to be made that certain functionality can only be achieved with async/await.

For me still does not work. It is insane !