1

How to parse this url

http://ioe.edu.np/exam/notices/8560Result%20Diploma%20I_I.jpg

correctly so that it can be used as source for imageview. I have tried encoding it using Uri.encode() but that didn't help.

Below is the code that I am refering to load image from url. Got it from Android, Make an image at a URL equal to ImageView's image

public class MainActivity extends Activity {

//  String imageUrl1 = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";

String imageUrl = Uri.encode("http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg");

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    try {
        ImageView i = (ImageView) findViewById(R.id.imageView1);
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                imageUrl).getContent());
        i.setImageBitmap(bitmap);
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

}

Please help me, and also note that I don't have the control over the name of the image files, so I will have to figure out a way so that it loads correctly in the imageview. If I replace the imageUrl with imageUrl1 then the image get loads. But with imageUrl, it seemed to be problem with space being encode to html entities. Please help me with this.

Thank you.

Community
  • 1
  • 1
Samundra
  • 1,869
  • 17
  • 28

2 Answers2

1

use

String imageUrl ="http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg";
    imageUrl =imageUrl .replaceAll(" ", "%20");

Sample code

String imageUrl ="http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg";
    imageUrl =imageUrl .replaceAll(" ", "%20");
    try {
        Bitmap bitmap = BitmapFactory.decodeStream((InputStream) new URL(
                imageUrl).getContent());

        ImageView im=new ImageView(this);
        im.setImageBitmap(bitmap);
        setContentView(im);
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
Mohsin Naeem
  • 12,542
  • 3
  • 39
  • 53
  • @eternalmatt you are right..I just give the solutions of his problem...:) – Mohsin Naeem Jul 16 '12 at 18:31
  • I got following error in my logcat. 07-17 00:25:20.596: D/skia(1064): --- decoder->decode returned false 07-17 00:25:20.598: D/MainActivity(1064): imageurl : http://ioe.edu.np/exam/notices/8560Result%20Diploma%20I_I.jpg The image I am trying to download is big in size that will not fit into the screen, can that be a problem. – Samundra Jul 16 '12 at 18:43
  • Similar question posted here http://stackoverflow.com/questions/9306034/decoder-decode-returned-false-when-download-image-and-view-it-in-imageview?rq=1. This guy also seems to have the similar problem but no replies there. So I guess, I will also have no answer then. – Samundra Jul 16 '12 at 18:54
  • I have edit my answer with my sample code. and it is working fine. try this – Mohsin Naeem Jul 16 '12 at 18:57
0

This code works in my case,

ImageView i = (ImageView) findViewById(R.id.imageView1);

try {
        URL url = new URL("http://ioe.edu.np/exam/notices/8560Result%20Diploma%20I_I.jpg");
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        i.setImageBitmap(myBitmap);
    } catch (IOException e) {
        e.printStackTrace();
    }

Also add permission in manifest file,

<uses-permission android:name="android.permission.INTERNET"/>

EDIT:

Also you can use UrlEncoder.

String urlString = URLEncoder.encode("http://ioe.edu.np/exam/notices/8560Result Diploma I_I.jpg");
url = new URL(urlString);
user370305
  • 108,599
  • 23
  • 164
  • 151
  • it will not work as `Url` have space characters..need to replace with "%20" – Mohsin Naeem Jul 16 '12 at 18:29
  • I tried the your suggestions but didn't work. I have kept the internet permission in my android manifest. Always getting the same error message `07-17 00:36:16.046: D/skia(1120): --- decoder->decode returned false` – Samundra Jul 16 '12 at 18:52