问题
I have created a Worklight application and added to it the Android environment. This application has a button to take a photo using the device camera, and an img
tag in the HTML which displays the captured photo.
I followed this PhoneGap Camera API.
Now I am trying to store that image into the SD Card but fail doing so. my
EDIT: I changed my code as below:
function takeimage() {
// Retrieve image file location from specified source
navigator.camera.getPicture(getImageURI, function(message) {
alert('Image Capture Failed');
}, {
quality : 40,
destinationType : Camera.DestinationType.FILE_URI
});
}
function getImageURI(imageURI) {
var gotFileEntry = function(fileEntry) {
var img=document.getElementById("thisImage");
img.style.visiblity="visible";
img.style.display="block";
img.src=imageURI;
alert("got image file entry: " + fileEntry.fullPath);
var gotFileSystem = function(fileSystem){
// copy the file
fileEntry.moveTo(fileSystem.root, "pic.jpg", null, null);
};
// get file system to copy or move image file to
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystem, fsFail);
};
//resolve file system for image
window.resolveLocalFileSystemURI(imageURI, gotFileEntry, fsFail);
}
//file system fail
function fsFail(error) {
alert("failed with error code: " + error.code);
}
Everything working fine(capturing image and image available in app cache folder) except moveTo
method.
fileEntry.moveTo(fileSystem.root, "pic.jpg", null, null);
I put fileSystem.root
in alert and I am getting Object object
. So the folder location is not available to move that image(And I think its the real problem).
回答1:
This, in fact, has got nothing to do with Worklight.
Since you are already using Apache Cordova to access the device's camera to snap a photo, you should also use it to store the image file to the device's SD Card.
Here are a couple of SO questions to point you to the right solution for you:
- How to move captured image in PhoneGap to a folder in sdcard
- Capturing and storing a picture taken with the Camera into a local database / PhoneGap / Cordova / iOS
Note #1: your link to the PhoneGap Camera API points to v1.0. Worklight 5.0.6.x uses PhoneGap 2.3.0, so be sure to use the correct API version.
Note #2: make sure you have added permission to write to the SD Card by adding the below line to the android.manifest file:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Note #3: in case #2 above is not enough, try getting the SD Card location like this:
File sdDir = Environment.getExternalStorageDirectory();
回答2:
use this option in your takePicture callback
{
saveToPhotoAlbum: true
};
Also use all other option too as per your requirement. This will save your image to photolibrary.
saveToPhotoAlbum: Save the image to the photo album on the device after capture. (Boolean)
It will save your image to sd card without writing any extra code.
来源:https://stackoverflow.com/questions/16360596/saving-image-to-sd-card-in-a-worklight-application