Android Square Picasso not load persian character image url

早过忘川 提交于 2019-12-12 11:31:58

问题


I want to create web application by square picasso, but if image url contains persian characters (ا،ب،ج،ی، ...) Picasso not load image.

This url not working:

        Picasso.with(mContext).load("http://www.shutterstock.ir/thumbs/10006/74601661-گربه-چشم-ابی-ولاغر-سیامی-در-یک-پس-زمینه-،-وکتور-سفید.jpg")
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.face_top_image).noFade().resize(100, 100)
    .into(imageView);    

This url work

        Picasso.with(mContext).load("http://www.shutterstock.ir/thumbs/10006/74601661-%DA%AF%D8%B1%D8%A8%D9%87-%DA%86%D8%B4%D9%85-%D8%A7%D8%A8%DB%8C-%D9%88%D9%84%D8%A7%D8%BA%D8%B1-%D8%B3%DB%8C%D8%A7%D9%85%DB%8C-%D8%AF%D8%B1-%DB%8C%DA%A9-%D9%BE%D8%B3-%D8%B2%D9%85%DB%8C%D9%86%D9%87-%D8%8C-%D9%88%DA%A9%D8%AA%D9%88%D8%B1-%D8%B3%D9%81%DB%8C%D8%AF.jpg")
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.face_top_image).noFade().resize(100, 100)
    .into(imageView);    

回答1:


You need to URI encode the URL.

See the docs

Uri.encode(url);

Or, if specifying certain allowed characters the following works:

private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
String urlEncoded = Uri.encode(path, ALLOWED_URI_CHARS);



回答2:


You need to encode your Url . So try this

URIUtil.encodeQuery(myUrl).

or also this one : http://developer.android.com/reference/java/net/URLEncoder.html

URLEncoder.encode(myUrl, "UTF-8");

Also there is an issue here




回答3:


just use from this function

 public static String encodUrl(String url){
            String[] splitUrl = url.split("/");
            String imageName = splitUrl[splitUrl.length-1];//get name of file
            String mainUrl = url.replaceAll(imageName , "");//get url without file name bacause dont need to encode

            return (mainUrl + Uri.encode(imageName));
        }


来源:https://stackoverflow.com/questions/27627568/android-square-picasso-not-load-persian-character-image-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!