NativeScript / nativescript-camera

NativeScript plugin to empower using device camera.
Apache License 2.0
93 stars 46 forks source link

i need to know how to get file name use this plugin #119

Closed xiemax33 closed 6 years ago

xiemax33 commented 6 years ago

i have problem to get file name from camera capture. any one can teach me how to get file name using this plugin

tgpetrov commented 6 years ago

Hi @jonathanhendra,

The camera plugin doesn't directly provide the path to the image. Here's a helper method that you can use to get the path from the ImageAsset that the takePicture method returns:

export function getImageFilePath(imageAsset): Promise<string> {
    return new Promise((resolve) => {
        if (ios) { // create file from image asset and return its path
            const tempFolderPath = knownFolders.temp().getFolder("nsimagepicker").path;
            const tempFilePath = path.join(tempFolderPath, `${Date.now()}.jpg`);
            const options = PHImageRequestOptions.new();

            options.synchronous = true;
            options.version = PHImageRequestOptionsVersion.Current;
            options.deliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat;

            PHImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler(imageAsset.ios, options, (nsData: NSData) => {
                nsData.writeToFileAtomically(tempFilePath, true);
                console.log('file:' + tempFilePath);
                resolve(tempFilePath);
            });
        }

        if (android) { // return imageAsset.android, since it's the path of the file
            console.log('file:' + imageAsset.android);
            resolve(imageAsset.android);
        }
    });
}

You will need the following imports:

import { knownFolders, path } from "file-system";
import { android, ios } from "tns-core-modules/application";