Android中的Cordova resolveLocalFileSystemURL错误代码1000

时间:2015-06-04 09:36:04

标签: android cordova

当我使用cordova函数resolveLocalFileSystemURL与某些类型的文件uri(如内容://com.android.providers.downloads.documents/document/some_doc)时,代码为1000时出错。我使用的是cordova 5.1和5版本安装在智能手机上的Android。

adb logcat显示了这个:

W/System.err(28721): java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
W/System.err(28721):    at org.apache.cordova.file.ContentFilesystem.getFileMetadataForLocalURL(ContentFilesystem.java:130)
W/System.err(28721):    at org.apache.cordova.file.Filesystem.exists(Filesystem.java:130)
W/System.err(28721):    at org.apache.cordova.file.FileUtils.resolveLocalFileSystemURI(FileUtils.java:624)
W/System.err(28721):    at org.apache.cordova.file.FileUtils.access$400(FileUtils.java:51)
W/System.err(28721):    at org.apache.cordova.file.FileUtils$14.run(FileUtils.java:383)
W/System.err(28721):    at org.apache.cordova.file.FileUtils$25.run(FileUtils.java:566)
W/System.err(28721):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
W/System.err(28721):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
W/System.err(28721):    at java.lang.Thread.run(Thread.java:818)

用cordova打开这个文件有什么办法吗?

1 个答案:

答案 0 :(得分:2)

我已经解决了这个问题。在Android 5中,光标现在是DocumentCursor而不是Media Cursor,这意味着它正在寻找错误的列。当找不到列时,插件会尝试将基元long设置为null,这会导致NullPointerException。

@Override
public JSONObject getFileMetadataForLocalURL(LocalFilesystemURL inputURL) throws FileNotFoundException {
    long size = -1;
    long lastModified = 0;
    Uri nativeUri = toNativeUri(inputURL);
    String mimeType = resourceApi.getMimeType(nativeUri);
    Cursor cursor = openCursorForURL(nativeUri);
    try {
        if (cursor != null && cursor.moveToFirst()) {
            size = resourceSizeForCursor(cursor);
            Long modified = lastModifiedDateForCursor(cursor);
            if (modified != null)
                lastModified = modified.longValue();
        } else {
            // Some content providers don't support cursors at all!
            CordovaResourceApi.OpenForReadResult offr = resourceApi.openForRead(nativeUri);
            size = offr.length;
        }
    } catch (IOException e) {
        throw new FileNotFoundException();
    } finally {
        if (cursor != null)
            cursor.close();
    }

    JSONObject metadata = new JSONObject();
    try {
        metadata.put("size", size);
        metadata.put("type", mimeType);
        metadata.put("name", name);
        metadata.put("fullPath", inputURL.path);
        metadata.put("lastModifiedDate", lastModified);
    } catch (JSONException e) {
        return null;
    }
    return metadata;
}

protected Long lastModifiedDateForCursor(Cursor cursor) {
    int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED);
    if (columnIndex == -1) {
        columnIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED);
    }
    if (columnIndex != -1) {
        String dateStr = cursor.getString(columnIndex);
        if (dateStr != null) {
            return Long.parseLong(dateStr);
        }
    }
    return null;
}