Phonegap版本:3.3

安卓版本:4.0.4

我正在共享我提供的文件uri。

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

最佳答案

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

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

09-10 01:09