Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
201 views
in Technique[技术] by (71.8m points)

android - Saving image to SD Card in a Worklight application

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...