2014-04-04 17 views
9

Mô tả sự cố: Tôi có thể truy cập bộ nhớ trong bằng tệp hoặc hệ thống tệp gốc (đọc và ghi). Nhưng một tập tin như vậy không thể truy cập từ một ứng dụng khác. Ví dụ: nếu tôi muốn gửi tệp này qua emailComposerPlugin, tệp không thể truy cập được bằng ứng dụng email khách. (Cùng với chức năng "mở với".) Nếu tôi thay đổi các tùy chọn {sandboxed: true} thành false (để ghi vào bộ nhớ ngoài), nó không hoạt động và kết thúc trong một FileUtils.UNKNOWN_ERR. Tôi đã thử các ứng dụng trong khi điện thoại bị ngắt kết nối từ USB, như một số tài liệu đề cập rằng lưu trữ bên ngoài không thể được truy cập trong khi gắn trên máy tính - kết quả tương tự mặc dù.Làm cách nào để truy cập bộ nhớ ngoài bằng các trình cắm thêm file/file-system-root của cordova?

Từ nội dung tôi đọc trên mailing list, điều này có thể thực hiện được. Có vẻ như tôi bỏ lỡ một điểm quan trọng?

Bối cảnh: Tôi cố gắng bật ứng dụng lai được tạo cho iPhone để chạy trên thiết bị Android. Để có một sân chơi nhỏ, tôi tạo ra một dự án thử nghiệm nhỏ.

Chỉnh sửa: Dường như có sự cố giữa tệp hệ thống gốc và plugin tệp. Nhưng tôi có phiên bản mới nhất của cả hai. (Tập tin: 1.0.1 File-system-rễ: 0.1.0) Gỡ rối các tập tin hệ thống và các lớp học tập cho thấy

private String fullPathForLocalURL(Uri URL) { 
    if (FILESYSTEM_PROTOCOL.equals(URL.getScheme()) && "localhost".equals(URL.getHost())) { 
     String path = URL.getPath(); 
     if (URL.getQuery() != null) { 
      path = path + "?" + URL.getQuery(); 
     } 
     return path.substring(path.indexOf('/', 1)); 
     // path = "/cache-external" at this point 
     // results in index out of bounds exception 

đã tôi đã cố gắng gì?

config.xml

<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external" /> 

AndroidManifest.xml

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

mã javascript

function createTextDocument(filename, text) { 

    cordova.filesystem.getDirectoryForPurpose('cache', {sandboxed: false}, successCallback, failureCallback); 

    function successCallback(directoryEntry){ 
     console.log('directory found (cordova): ' + directoryEntry.toURL()); 
     console.log('directory found (native) : ' + directoryEntry.toNativeURL()); 
     directoryEntry.getFile(filename, {create: true, exclusive: false}, 
      function(fileEntry){ 
       var filePath = fileEntry.toNativeURL(); 
       fileEntry.createWriter(
        function(fileWriter){ 
         console.log('start writing to: ' + filePath); 
         fileWriter.write(text); 
         console.log('file written'); 
        },failureCallback 
       ); 
      }, failureCallback 
     ); 
    } 

    function failureCallback(error){ 
     console.log('error creating file: ' + error.code); 
     // results in code 1000 
    } 
} 

Trả lời

13

Sau khi đào bới toàn bộ chủ đề này khá một chút, tôi đã tìm ra:

  • Không cần plugin gốc của hệ thống tệp.
  • Có nhiều cấu hình cần thiết hơn trong config.xml.
  • Bạn không cần sử dụng theo cách FileApi tiêu chuẩn, nhưng sau đây để truy cập các tệp. sử dụng

JavaScript:

window.resolveLocalFileSystemURL(path, cbSuccess, cbFail); 

@param path: {string} a cordova path with scheme: 
      'cdvfile://localhost/<file-system-name>/<path-to-file>' 
      Examples: 'cdvfile://localhost/sdcard/path/to/global/file' 
         'cdvfile://localhost/cache/onlyVisibleToTheApp.txt' 
@param cbSuccess: {function} a callback method that receives a DirectoryEntry object. 
@param cbFail: {function} a callback method that receives a FileError object. 

config.xml

<preference name="AndroidPersistentFileLocation" value="Compatibility" /> 
<preference name="AndroidExtraFilesystems" value="sdcard,cache" /> 

AndroidManifest.xml

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

bạn có thể cung cấp một ví dụ làm việc đầy đủ của tập tin mới ghi vào sdcard bên ngoài? –

+1

Tôi đã sử dụng mã này: 'window.resolveLocalFileSystemURL ('file: /// storage/extSdCard /', function (fs) {var nativePath = fs.toURL(); alert (nativePath);}, function (err) {alert (err);}); ' –

+0

Không chắc vấn đề của tôi tương tự như thế nào. Nhưng có AndroidPersistentFileLocation để khả năng tương thích giải quyết vấn đề với hệ thống tệp của Android cho cordova v5 + –

5

Sau giờ vật lộn, cuối cùng tôi có thể quét sdcard và truy cập vào các tập tin bằng cách cung cấp một thư mục như "files: // pat htofile/".
Tôi đã nộp API và một dự án mẫu https://github.com/xjxxjx1017/cordova-phonegap-android-sdcard-full-external-storage-access-library

Ví dụ về sử dụng API

new ExternalStorageSdcardAccess(fileHandler).scanPath("file:///storage/sdcard1/music"); 
function fileHandler(fileEntry) { 
    console.log(fileEntry.name + " | " + fileEntry.toURL()); 
} 

Mã nguồn API

var ExternalStorageSdcardAccess = function (_fileHandler, _errorHandler) { 

    var errorHandler = _errorHandler || _defultErrorHandler; 
    var fileHandler = _fileHandler || _defultFileHandler; 
    var root = "file:///"; 

    return { 
     scanRoot:scanRoot, 
     scanPath:scanPath 
    }; 

    function scanPath(path) { 
     window.resolveLocalFileSystemURL(path, _gotFiles, errorHandler); 
    } 

    function scanRoot() { 
     scanPath(root); 
    } 

function _gotFiles(entry) { 
    // ? Check whether the entry is a file or a directory 
    if (entry.isFile) { 
     // * Handle file 
     fileHandler(entry); 
    } 
    else { 
     // * Scan directory and add media 
     var dirReader = entry.createReader(); 
     dirReader.readEntries(function(entryList) { 
      entryList.forEach(function (entr) { 
       _gotFiles(entr); 
      }); 
     }, errorHandler); 
    } 
} 

    function _defultFileHandler(fileEntry){ 
     console.log("FileEntry: " + fileEntry.name + " | " + fileEntry.fullPath); 
    } 
    function _defultErrorHandler(error){ 
     console.log('File System Error: ' + error.code); 
    } 
}; 

Configurations

  • config.xml
    ưu tiên xóa: tên sở thích = "AndroidExtraFilesystems"

  • đảm bảo môi trường thử nghiệm có thể truy cập nó lưu trữ bên ngoài riêng tại thời điểm kiểm tra. (Ep nếu bạn kết nối nó với usb, chắc chắn rằng nó được kết nối như một máy ảnh; Gọi API sau recieving sự kiện "deviceready")

Đường dẫn ví dụ:
file: ///
file: /// somefile/
file: /// somefile
file: ///somefile/music/aaaaa.mp3

+0

Tôi đã gửi url được tạo cho một bản nhạc mp3 cho thể loại cordova-plugin-media. Plugin không phát. Có sự khác biệt nào về URI thực sự của tệp không? – arjun

Các vấn đề liên quan