-1

I want to load an image from a URL but it doesn't work because the link doesn't have an extension

Can this be solved???

URL example : http://t0.gstatic.com/images?q=tbn:ANd9GcRf1EqE2kyW12HSb9gZZ8eTIPqNgVkjFis4GkTTYONIpoQtkIde4zybZ4iAqGlIHQ_pnEX499Oa

How can this be done??

public class MainActivity extends ActionBarActivity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ImageView img = (ImageView)findViewById(R.id.imageView1);
    Bitmap bitImg = getBitmapFromURL("http://t0.gstatic.com/images?q=tbn:ANd9GcRf1EqE2kyW12HSb9gZZ8eTIPqNgVkjFis4GkTTYONIpoQtkIde4zybZ4iAqGlIHQ_pnEX499Oa");
    img.setImageBitmap(bitImg);



}

public Bitmap getBitmapFromURL(String imageUrl) {
    try {
        URL url = new URL(imageUrl);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
hsherif
  • 1
  • 3
  • `doesn't work because the link doesn't have an extension` what would make you think that? – njzk2 Sep 16 '14 at 20:42
  • possible duplicate of [NetworkOnMainThreadException](http://stackoverflow.com/questions/5150637/networkonmainthreadexception) – njzk2 Sep 16 '14 at 20:42

2 Answers2

0

look at this post, i dont see his URL but i think Android libraries figure it out based on the data and headers:

BitmapFactory.decodeStream returning null when options are set

also, don't add an extension to a URL, it changes the URL!

http://www.google.com is not the same as http://www.google.com.html

Community
  • 1
  • 1
Dave
  • 867
  • 6
  • 11
0

load an image from a URL but it doesn't work because the link doesn't have an extension. Nonsense. Url.openconnection does not look at extensions. It does not work because you have a NetworkOnMainThreadException clearly visible in the LogCat. Never seen in the LogCat? Please go and look for it. Place your network code in an AsyncTask or thread to prevent this.

greenapps
  • 11,154
  • 2
  • 16
  • 19