0

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).

Community
  • 1
  • 1
AzAh
  • 41
  • 1
  • 1
  • 14

2 Answers2

0

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:

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();
Community
  • 1
  • 1
Idan Adar
  • 44,156
  • 13
  • 50
  • 89
  • Thanks Idan, in second example link [here](http://stackoverflow.com/questions/10335563/capturing-and-storing-a-picture-taken-with-the-camera-into-a-local-database-ph) I am getting this error in my log `05-04 00:11:33.521: D/CordovaLog(946): Uncaught TypeError: Wrong type for parameter "uri" of resolveLocalFileSystemURI: Expected String, but got global.` – AzAh May 03 '13 at 18:44
  • Edit your question with the code you've written to handle the image saving. – Idan Adar May 04 '13 at 12:27
  • Hi Idan, surprise to see you are working on weekend, I edited my post please check the code.I am able to click the picture and it saving on cache but moving image is not working. Thanks – AzAh May 04 '13 at 21:39
  • HI Idan, still itching my hair. I have tried everything but its not working. can you just givea code to move my image from cache to any folder(eg `"file:///mnt/sdcard/Android/data/com.camera1.123324323.jpg"` to `"file:///mnt/sdcard"`) – AzAh May 05 '13 at 20:43
  • cause moving is the only problem i am facing. – AzAh May 05 '13 at 20:43
  • IDAN Grrreat. without a single change its working. Image was not showing on AVD gallery(don'tknow why). I Highly appreciate your help. :-) You are Worklight King. PS- I checked those Images in DDMS Prospective in Worklight and they were there. – AzAh May 05 '13 at 22:52
0

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.

R_Dhorawat
  • 141
  • 1
  • 1
  • 9