Phonegap:无法将文件复制到PERSISTENT存储

时间:2014-02-21 22:02:49

标签: cordova

我有这样的手机密码:

onDeviceReady: function() {
  window.resolveLocalFileSystemURI('file:///storage/sdcard0/Android/data/com.example.my_app/cache/1392988146844.jpg', 
    function(entry){
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0,
            function(fileSys) {
                    fileSys.root.getDirectory( 'MyAppFolder2' , {create:true, exclusive: false},    
                    function(directory) {
                        entry.copyTo(directory, 'newFile.jpg', function(entryFile){
                            alert("Success. New image uri: " + entryFile.fullPath);
                        }, fail); 

                    }, fail);
            }, fail); 
    }, fail );
}

如果运行此代码我有错误1。 但是,如果我使用LocalFileSystem.TEMPORARY而不是LocalFileSystem.PERSISTENT运行此代码,则没有错误。 我在三星Galaxy标签上运行它。

我做错了什么? 为什么文件不想移动?

请帮忙!

编辑1:这是我的AndroidManifest.xml

<?xml version='1.0' encoding='utf-8'?>
<manifest android:hardwareAccelerated="true" android:versionCode="1" android:versionName="1.0.0" android:windowSoftInputMode="adjustPan" package="com.example.my_app" xmlns:android="http://schemas.android.com/apk/res/android">
<supports-screens android:anyDensity="true" android:largeScreens="true" android:normalScreens="true" android:resizeable="true" android:smallScreens="true" android:xlargeScreens="true" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:debuggable="true" android:hardwareAccelerated="true" android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale" android:label="@string/app_name" android:name="MyApp" android:theme="@android:style/Theme.Black.NoTitleBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>

编辑2:很奇怪,但我可以使用getFile目录录入功能在PERSISTENT存储中创建文件:

directory.getFile("readme.jpg", {create: true, exclusive: false}, function(entry222) {alert('New file');}, fail);

编辑3:我现在使用的丑陋解决方案。我写了自己的移动文件功能。有用。但这是丑陋的代码。

function capturePhoto() {
  navigator.camera.getPicture(onSuccess, fail, { quality: 50, destinationType: Camera.DestinationType.FILE_URI});
};

function onSuccess(imageURI) {
  var timestamp = new Date().getTime();
  var fileName = timestamp + '.jpg';
  moveImageUriFromTemporaryToPersistent(imageURI, fileName, function(newImageURI) {
      var image = document.getElementById('my_img_id');
      image.src = newImageURI;
  });
};

function fail(message) {
  // Do nothing.
  alert('Error');
}

function moveImageUriFromTemporaryToPersistent(imageURI, newFileName, callbackFunction) {
  window.resolveLocalFileSystemURI(imageURI, function(temporaryEntry) {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(persistentFileSys) {
      persistentFileSys.root.getDirectory('PersistentDir2', {create: true, exclusive: false}, function(persistentDirectory) {
        persistentDirectory.getDirectory('subdir1', {create: true, exclusive: false}, function(photoDirectory) {
          photoDirectory.getFile(newFileName, {create: true, exclusive: false}, function(persistentEntry) {
            temporaryEntry.file(function(oldFile) {
              var reader = new FileReader();
              reader.onloadend = function(evt) {
                persistentEntry.createWriter(function(writer) {
                  writer.onwrite = function(evt) {
                    temporaryEntry.remove();
                    callbackFunction(persistentEntry.toURL());
                  };
                  writer.write(evt.target.result);
                }, fail);
              };
              reader.readAsArrayBuffer(oldFile);
            }, fail);
          }, fail);
        }, fail);
      }, fail);
    }, fail);
  }, fail);
}

1 个答案:

答案 0 :(得分:0)

您是否在Manifest中设置了权限?