科尔多瓦如何访问文件:///BitmapFactory形象 - How to access cordova file:/// image with BitmapFactory

- 此内容更新于:2015-12-13
主题:

科尔多瓦我使用谷歌地图插件和发展某种形式的图像缓存显示标记。标记可以显示图像从http/base64或者/www/路径。我使用https://github.com/markmarijnissen/cordova-file-cache缓存。缓存插件能够返回base64但是我不想依赖这一点,相反,我想访问toInternalURL()函数。我设法把这件工作在iOS上:我改变了路径(剥离文件://)。现在我想做同样的为Android(Java)。我熟悉Java语法,但我太没经验,完全api。我想我需要检查indexOf(“文件://”)在一个elseif语句,但下一个什么?如何改变/编辑,需要调用哪个函数呢?非常感谢!

原文:

I'm using a cordova google maps plugin and developed some sort of image cache for displaying markers.

The marker can display images from http / base64 or from /www/path.

I use https://github.com/markmarijnissen/cordova-file-cache for caching. The cache plugin is able to return base64 but I don't want to rely on this, instead, I'd like to access the toInternalURL() function.

I've managed to get this work on iOS with:

    NSRange replaceRange = [iconPath rangeOfString:@"file://"];

    if (replaceRange.location != NSNotFound){
        iconPath = [iconPath stringByReplacingCharactersInRange:replaceRange withString:@""];
        image = [UIImage imageWithContentsOfFile:iconPath];

    } else {
        image = [UIImage imageNamed:iconPath];
    }

    if (width && height) {
      image = [image resize:width height:height];
    }

I've changed the path (stripped out file://). Now I'm trying to do the same for Android (Java). I'm familiar with the Java syntax but I'm totally unexperienced with the APIs.

  if (iconUrl.indexOf("http") == -1) {
  Bitmap image = null;

  if (iconUrl.indexOf("data:image/") > -1 && iconUrl.indexOf(";base64,") > -1)   {
    String[] tmp = iconUrl.split(",");
    image = PluginUtil.getBitmapFromBase64encodedImage(tmp[1]);
  } else {
    AssetManager assetManager = this.cordova.getActivity().getAssets();
    InputStream inputStream;
    try {
      inputStream = assetManager.open(iconUrl);
      image = BitmapFactory.decodeStream(inputStream);
    } catch (IOException e) {
      e.printStackTrace();
      callback.onMarkerIconLoaded(marker);
      return;
    }
  }

I guess I need to check for indexOf('file://') in an else-if-statement - but what next? How to change/edit and which function do I need to call? Thank you very much!