移动/复制捕获的图像phonegap android的问题

时间:2014-03-04 06:35:50

标签: java android cordova cordova-3

Phonegap版本:3.3

Android版本:4.0.4

我正在分享文件uris,以供参考。

FILE_URI = content:// media / external / images / media / 11679

FILE_URI = file:///mnt/sdcard/Android/data/com.allplugins/cache/1393913804418.jpg

将捕获的图像移动到SD卡时出错。我也尝试了window.resolveLocalFileSystemURLwindow.resolveLocalFileSystemURI,但没有一个能为我工作。

一旦我尝试将相机拍摄的图像移动到SD卡,我得到错误代码'1'。

错误记录

> 03-04 12:02:54.482: I/Web Console(27289): Info : got file entry
> ![object Object] at file:///android_asset/www/js/me/view/demo.js:383
> 
> 03-04 12:02:54.482: W/System.err(27289):  at
> org.apache.cordova.file.FileUtils.access$8(FileUtils.java:583)
> 
> 03-04 12:02:54.482: W/System.err(27289):  at
> org.apache.cordova.file.FileUtils$21.run(FileUtils.java:407)
> 
> 03-04 12:02:54.482: W/System.err(27289):  at
> org.apache.cordova.file.FileUtils$24.run(FileUtils.java:473)
> 
> 03-04 12:02:54.482: W/System.err(27289):  at
> java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
> 
> 03-04 12:02:54.482: W/System.err(27289):  at
> java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
> 
> 03-04 12:02:54.482: W/System.err(27289):  at
> java.lang.Thread.run(Thread.java:856)
> 
> 03-04 12:02:54.483: D/IPCThreadState(27289): [DN #5]
> BR_CLEAR_DEATH_NOTIFICATION_DONE cookie 0x1b36d10
> 
> 03-04 12:02:54.574: D/CordovaLog(27289):
> file:///android_asset/www/js/me/view/demo.js: Line 384 : Error : file
> move !{"code":1}
> 
> 03-04 12:02:54.574: I/Web Console(27289): Error : file move
> !{"code":1} at file:///android_asset/www/js/me/view/demo.js:384

我的代码

function PhotoKeeper() {

    var name_of_image = "";
    var pictureSource,destinationType;
    var selectedSourceType;
    var fileSysRoot,projectImageDirectory,projectImageDirectoryName = "temp";

    init();
    function init(){
        destinationType = window.Camera.DestinationType;
        pictureSource = window.Camera.PictureSourceType;

        window.requestFileSystem(
            LocalFileSystem.PERSISTENT,
            0,
            // got file system sucess callback
            function (arg_o_filesys){

                fileSysRoot = arg_o_filesys.root;

                arg_o_filesys.root.getDirectory(
                    projectImageDirectoryName,
                    {
                        create: true,
                        exclusive: false
                    },
                    // got directory entry sucess callback
                    function(arg_o_directoryEntry) {
                        console.log("Info : got directory entry !");
                        projectImageDirectory = arg_o_directoryEntry;
                    },
                    errorPhoto
                );

            },
            errorPhoto
        );
    }

    function getPhotoFromLibreary(){

        selectedSourceType = pictureSource.PHOTOLIBRARY;

        window.navigator.camera.getPicture(
            gotPhoto,
            errorPhoto,
            { 
                "quality": 50,
                "destinationType": destinationType.FILE_URI,
                "sourceType": selectedSourceType
            }
        );
    }

    function getPhotoFromCamera(){

        selectedSourceType = pictureSource.CAMERA;

        window.navigator.camera.getPicture(
            gotPhoto,
            errorPhoto,
            { 
                "quality": 50,
                "destinationType": destinationType.FILE_URI,
                "sourceType": selectedSourceType
            }
        );

    }

    function gotPhoto(fileuri){
        window.resolveLocalFileSystemURL(fileuri, gotFileEntry, errorPhoto);
    }

    // got fileEntry sucess callback
    function gotFileEntry(arg_o_fileEntry){
        console.log("Info : got file entry !"+arg_o_fileEntry);
        arg_o_fileEntry.copyTo( projectImageDirectory, name_of_image , function(){ console.log("Info : file moved !") } , function(e){ console.log("Error : file move !"+e); } );
    }

    function errorPhoto(msg){
        console.log("Error : "+JSON.stringify(msg));
    }

    function updateName (argname_of_image) {

        if((utils.isUndefined(argname_of_image) === false) && (typeof argname_of_image == "string")){
            name_of_image = argname_of_image;
        }else{
            name_of_image = Date.now().toString();
        }

    }

    return{
        getPhotoFromLibreary : function (name_of_image) {
            updateName(name_of_image);
            getPhotoFromLibreary();
        },
        getPhotoFromCamera : function (name_of_image) {
            updateName(name_of_image);
            getPhotoFromCamera();
        }
    }
}

2 个答案:

答案 0 :(得分:0)

确保您在清单文件中添加了适当的权限,以读取和写入外部存储。如果没有将以下代码添加到清单文件中。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

答案 1 :(得分:0)

问题在于phonegap 3.3。 我用phonegap 3.4测试了我的代码,它就像魅力一样。