本文介绍了不能用科尔多瓦文件系统API在Android内部内存中加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从使用的Andr​​oid 6.2工作灯(科尔多瓦3.4)开发Web应用程序

我不能够使用加载图像科尔多瓦文件系统API:

I am not able to load image using Cordova file system api:

function onFileSystemSuccess(fileSystem) {

    var filePath = "UserSignature/SignatureImage.png";
    fileSystem.root.getFile(filePath, {create: true}, function gotFileEntry(fileEntry){
        fileEntry.file(function gotFile(file){
            var reader = new FileReader();
            reader.onloadend = function(evt) {
               alert("Read as data URL");
               document.getElementById("config_SignatureImg").src = evt.target.result;
            };
            reader.readAsDataURL(file);

        }, function fail(evt){

        });
    }, function fail(message){
        alert("failed");
    });

}

然而,它的工作原理,当我将它保存在SD卡中。我不是在SD卡存储它的安全限制。

However, it works when I save it in the sdcard. I am not storing it in the SDCard for security constraints.

我是通过本地code做图像的保存如下:

I am doing the saving of image through native code as follows:

String folderName = "UserSignature";
File parentDirectory = new File(this.getContext().getFilesDir(), folderName);

if(!parentDirectory.exists()){
    parentDirectory.mkdirs();
}

// Save the signature as image to file  
Bitmap bm = Bitmap.createBitmap(this.getDrawingCache());
saveSignatureAsImage(bm, parentDirectory, getSignatureFileName());

private void saveSignatureAsImage(Bitmap bm, File dir, String name) {
    File signature = new File(dir, name + ".png");

    // Save the signature file to sd card
    try {
        FileOutputStream out = new FileOutputStream(signature);
        bm.compress(Bitmap.CompressFormat.PNG, 90, out);
        out.flush();
        out.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
    setSignatureFilePath(dir.getPath());
    Log.e("AMBW", "Succesfully saved signature in dir" + dir.getPath());
    Log.e("AMBW", "Succesfully saved signature in file" + signature.getPath());
}

我真的AP preciate如果有人可以帮助我走出这个问题。

I would really appreciate if someone could help me out with this issue.

推荐答案

我不知道你在哪里实际上是试图读取该文件。如果你正在阅读的文件是在诸如/ WWW的资源文件夹,那么你不能使用科尔多瓦文件API访问它。检查此链接了解更多信息。

I am not sure where you are actually trying to read the file. If the file you are reading is in the asset folder like /www then you cant access it using the Cordova file API. Check this link for more information. http://stackoverflow.com/a/8966227/3456790

不过,你可以使用一个科尔多瓦插件读​​取使用本地code的文件。你可以做这样的事情。

However, you could use a cordova plugin to read a file using native code. You could do something like this.

AssetManager assetManager = cordova.getActivity().getAssets();
inputStream = assetManager.open("www/default/data/" + fileName + ".json" );
        if(inputStream != null) {
            buffer = new byte[inputStream.available()];
            inputStream.read(buffer);

            logger.info("Found file " + fileName);

            jsonResult  = new JSONArray(new String(buffer, "UTF-8"));
        }

希望给你一些想法。

Hope that gives you some ideas.

这篇关于不能用科尔多瓦文件系统API在Android内部内存中加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 00:50