Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Question about new ImagePicker and transferring additional data

ANSWERED

I'm using the new ImagePicker functionality based on the example in the Fitbit blog. I can get it to work but Have a question.When transferring the image from the companion to the device, is there a way to also pass the settings key with the image? I want to be able to save 2 images but when the image is sent from the companion there is currently no way to know if it is for image 1 or image 2.

This is the function that is transferring the image.

function compressAndTransferImage(settingsValue) {
  const imageData = JSON.parse(settingsValue);
  Image.from(imageData.imageUri)
    .then(image =>
      image.export("image/jpeg", {
        background: "#FFFFFF",
        quality: 40
      })
    )
    .then(buffer => outbox.enqueue(`${Date.now()}.jpg`, buffer))
    .then(fileTransfer => {
      console.log(`Enqueued ${fileTransfer.name}`);
    });
}
Best Answer
1 BEST ANSWER

Accepted Solutions

No problem. Don't forget to delete your old image, otherwise you'll run out of space.

 

https://github.com/Fitbit/sdk-photo-picker/blob/master/app/index.js#L44

 

View best answer in original post

Best Answer
0 Votes
7 REPLIES 7

Figured it out, I pass in the image name based on the settings key. Here's the updated function.

settingsStorage.onchange = function(evt) {
if (evt.key === "ccard-front-image" ) {
compressAndTransferImage(evt.newValue, "frontImage.jpg");
} else if (evt.key === "ccard-back-image") {
compressAndTransferImage(evt.newValue, "backImage.jpg");
} };
sendVal(data);
};

function compressAndTransferImage(settingsValue, imageName) { const imageData = JSON.parse(settingsValue); Image.from(imageData.imageUri) .then(image => image.export("image/jpeg", { background: "#FFFFFF", quality: 40 }) ) .then(buffer => outbox.enqueue(imageName, buffer)) .then(fileTransfer => { console.log(`Enqueued ${fileTransfer.name}`); }); }
Best Answer
0 Votes

You might need to do some magic on the device so the file has a unique name. The UI won't update just by replacing the image.

Best Answer
0 Votes

It works, I pass a unique name for the images to the compressAndTransferImage function. I base the image name on the setting key for the image.

Best Answer
0 Votes

I think I spoke too soon. After reading your reply, I tried updating the image and you are correct, it did not work correctly. I added the date to the image name so it will be unique.

 

thanks

Best Answer
0 Votes

No problem. Don't forget to delete your old image, otherwise you'll run out of space.

 

https://github.com/Fitbit/sdk-photo-picker/blob/master/app/index.js#L44

 

Best Answer
0 Votes

So, fs.unlinkSync(mySettings.bg); will delete the old image?

Best Answer
0 Votes

If you replace mySettings.bg with whatever the old filename was.

Best Answer
0 Votes