Search code examples
androidimageencodingurl-encoding

images having arabic names with encoding not opening


I am making an Android app where I have image url that contain arabic characters.

Below is the code I am using...

Log.e(TAG, "Video Image -- > " + Uri.encode(data.get(position).getPhoto().toString()));
Picasso.with(act).load(Uri.encode(data.get(position).getPhoto().toString()))
        .resize(pW, pH)
        .centerCrop()
        .skipMemoryCache()
        .error(R.drawable.img_table_view)
        .placeholder(R.drawable.img_table_view)
        .into(viewHolder.image);

The sample URL for as below.

http://www.ssss.com/Karikatir/سبسي3ILAWESVVSKNVMWYLLTFWRASZ.jpg

The output of Log is as below.

10-19 10:00:08.038  25617-25617/com.sss.app E/VideosAdapter﹕ Video Image -- > http%3A%2F%2Fwww.ssss.com%2FKarikatir%2F3ILAWESVVSKNVMWYLLTFWRASZ.jpg

Any idea why image is not displaying even after encoding url?


Solution

  • Finally below is what I did...

    Step 1 : Extract image name from URL

    String imageName = "http://www.sss.com/test.png";
    imageName = imageName.replaceAll("http://www.sss.com/", "");
    

    Step 2 : Encode this image name instead of full URL

    String encodedURL = "http://www.sss.com/" + Uri.encode(imageName);
    
    Picasso.with(act).load(encodedURL)
            .resize(pW, pH)
            .centerCrop()
            .skipMemoryCache()
            .error(R.drawable.img_table_view)
            .placeholder(R.drawable.img_table_view)
            .into(viewHolder.image);
    

    So encoded url will be as below.

    String encodedURL = "http://www.sss.com/" + Uri.encode(imageName);
    

    Note : If there are spaces in URL you will need to update code as below.

    String encodedURL = "http://www.sss.com/" + Uri.encode(imageName).replaceAll(" ", "%20");
    

    Don't replace space with %20 before encoding as it will encode %20 which will go to incorrect url. So replacing spaces with %20 will be after encoding.

    Posting the answer so it will help someone as I don't find solution online...