Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
YisaHsu opened this issue Jul 20, 2022 · 0 comments
Open

Comments

@YisaHsu
Copy link

YisaHsu commented Jul 20, 2022

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);
        }
    );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant