terikon / cordova-plugin-photo-library

Maintainer needed. Please contact if you're using this library in your project
MIT License
149 stars 295 forks source link

img src = cdvphotolibrary://... err_unknown_url_scheme && android sdk > 30 #209

Open YisaHsu opened 1 year ago

YisaHsu commented 1 year ago

My Solution:

Modify PhotoLibrary.java :

start : package com.terikon.cordova.photolibrary;

import androidx.annotation.Nullable; import androidx.annotation.NonNull; import android.webkit.WebResourceResponse; import androidx.webkit.WebViewAssetLoader; import android.util.Log; import java.io.File;

end: private static JSONObject createGetLibraryResult(ArrayList library, int chunkNum, boolean isLastChunk) throws JSONException { JSONObject result = new JSONObject(); result.put("chunkNum", chunkNum); result.put("isLastChunk", isLastChunk); result.put("library", new JSONArray(library)); return result; }

@Override public CordovaPluginPathHandler getPathHandler() { return new CordovaPluginPathHandler(new WebViewAssetLoader.PathHandler() { @Nullable @Override public WebResourceResponse handle(@NonNull String path) { Log.d(TAG, "Path Handler " + path); //e.g. cdvphotolibrary/thumbnail/photoId=3112&width=512&height=384&quality=0.8 if(path.startsWith(PHOTO_LIBRARY_PROTOCOL)) { path = path.replaceAll("^cdvphotolibrary/", "cdvphotolibrary://"); path = path.replaceAll("thumbnail/", "thumbnail?"); path = path.replaceAll("photo/", "photo?"); Uri uri = Uri.parse(path); Log.d(TAG, "URI " + uri); Uri remappedUri = remapUri(uri); Log.d(TAG, "RemappedUri " + uri); if(remappedUri != null) { try { CordovaResourceApi.OpenForReadResult result = handleOpenForRead(remappedUri); Log.d(TAG, "Result " + result.inputStream.available()); return new WebResourceResponse(result.mimeType, "utf-8", result.inputStream); } catch (IOException e) { LOG.e(TAG, "error open cdvphotolibrary resource "+ e); } } }

    if(path.startsWith("__file__/")) {
      File file = new File(path.replaceAll("^__file__/", "/"));
      Uri fileUri = Uri.fromFile(file);
      Log.d(TAG, "fileUri " + fileUri);
      String mimeType = webView.getResourceApi().getMimeType(fileUri);
      Log.d(TAG, "mimeType " + mimeType);
      try {
        InputStream is = getContext().getContentResolver().openInputStream(fileUri);
        Log.d(TAG, "Result " + is.available());
        return new WebResourceResponse(mimeType, "utf-8", is);
      } catch(Exception e) {
        LOG.e(TAG, "error open file resource "+ e);
      }
    }
    return null;
  }
});

}

}

and modify javascript code:

    cordova.plugins.photoLibrary.getThumbnailURL(
        row.libraryItem_id, //libraryItem or libraryItem.id
        function (thumbnailURL) {
            thumbnailURL = thumbnailURL.replace("cdvphotolibrary://", "/cdvphotolibrary/").replace("thumbnail?", "thumbnail/");
            $("img[name='" + ID + "']").attr('src', thumbnailURL);
        },
        function (err) {
            console.log("ERROR: openPhoto, " + err);
        }
    );