2

I try to get a photo from a url but i could not get the photo.

Current Code:

public class IndexActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    ImageView i = (ImageView) findViewById(R.id.imageView1);
    Bitmap bitmap = DownloadImage("http://www.gophoto.it/view.php?i=http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png");
    i.setImageBitmap(bitmap);
}

private InputStream OpenHttpConnection(String urlString) throws IOException {
    InputStream in = null;
    int response = -1;

    URL url = new URL(urlString);
    URLConnection conn = url.openConnection();

    if (!(conn instanceof HttpURLConnection))
        throw new IOException("Not an HTTP connection");

    try {
        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();
        response = httpConn.getResponseCode();
        if (response == HttpURLConnection.HTTP_OK) {
            in = httpConn.getInputStream();
        }
    } catch (Exception ex) {
        throw new IOException("Error connecting");
    }
    return in;
}

private Bitmap DownloadImage(String URL) {
    Bitmap bitmap = null;
    InputStream in = null;
    try {
        in = OpenHttpConnection(URL);
        bitmap = BitmapFactory.decodeStream(in);
        in.close();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    return bitmap;
    }
}

and this the the locCat :

08-01 08:53:17.818: D/ActivityThread(12315): handleResumeActivity now pri:0
08-01 08:53:17.818: D/ActivityThread(12315): handleResumeActivity set pri:0
08-01 08:53:20.268: W/System.err(12315): java.io.IOException: Error connecting
08-01 08:53:20.268: W/System.err(12315):    at tr.com.turkcell.shmobile.IndexActivity.OpenHttpConnection(IndexActivity.java:70)
08-01 08:53:20.268: W/System.err(12315):    at tr.com.turkcell.shmobile.IndexActivity.DownloadImage(IndexActivity.java:79)
08-01 08:53:20.268: W/System.err(12315):    at tr.com.turkcell.shmobile.IndexActivity.onCreate(IndexActivity.java:32)
08-01 08:53:20.268: W/System.err(12315):    at android.app.Activity.performCreate(Activity.java:5008)
08-01 08:53:20.268: W/System.err(12315):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread.access$600(ActivityThread.java:130)
08-01 08:53:20.268: W/System.err(12315):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
08-01 08:53:20.278: W/System.err(12315):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-01 08:53:20.278: W/System.err(12315):    at android.os.Looper.loop(Looper.java:137)
08-01 08:53:20.278: W/System.err(12315):    at android.app.ActivityThread.main(ActivityThread.java:4754)
08-01 08:53:20.278: W/System.err(12315):    at java.lang.reflect.Method.invokeNative(Native Method)
08-01 08:53:20.278: W/System.err(12315):    at java.lang.reflect.Method.invoke(Method.java:511)
08-01 08:53:20.278: W/System.err(12315):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
08-01 08:53:20.278: W/System.err(12315):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
08-01 08:53:20.278: W/System.err(12315):    at dalvik.system.NativeStart.main(Native Method)
08-01 08:53:20.278: D/ActivityThread(12315): handleResumeActivity now pri:0
08-01 08:53:20.278: D/ActivityThread(12315): handleResumeActivity set pri:0
Raptor
  • 53,206
  • 45
  • 230
  • 366
mstfdz
  • 2,616
  • 3
  • 23
  • 27
  • Have you try different url? The one in your code is wrong. It should be look like this `http://1.bp.blogspot.com/-2LTvCCufBKc/T3L3KgcTj2I/AAAAAAAABbQ/Ki60e1LU9sE/s1600/Sachin%2BTendulkar.png` – Gina Aug 01 '13 at 06:06
  • I have tried different url but it didnt change anything – mstfdz Aug 01 '13 at 06:09
  • The logcat isn't useful because your `throw new IOException("Error connecting");` hides the original exception. Either remove that `catch` block altogether or pass the original `ex` to the newly created `IOException` constructor. – laalto Aug 01 '13 at 06:19
  • Use Asynctask for Http connection. – Bhoomika Brahmbhatt Aug 01 '13 at 06:22

6 Answers6

1

What you are doing wrong is calling long running task on main UI thread.

Use AsynckTask's onPostExecute method for downloaded image bitmap. Bind your ImageView object with the returned bitmap.

Clint
  • 1,014
  • 1
  • 10
  • 15
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45
1

Try out this Simple one:

public static Drawable LoadImageFromWebOperations(String url) {
    try {
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable d = Drawable.createFromStream(is, "src name");
        return d;
    } catch (Exception e) {
        return null;
    }
}

Its online getting so Check your Internet Permission:

add permission:

<uses-permission android:name="android.permission.INTERNET" />
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

Best Tutorial for Android Image Loading from URL:

Try : this code will be helpful to you

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
0

There are following possibilities regarding to Load Image

1- Image is Protected with Basic Authentication

2- URL is invalid

1st change the URL to confirm that your action is perfect and you will see image.

In second scenario i will suggest you to use AQUERY (Android Query) Download the latest jar add it to your project and use in such a way to by pass the basic Authentication

private AQuery mAquery;
mAquery=new AQuery(context);
BasicHandle handle = new BasicHandle("Username", "password");
mAquery.id(YourImageView).auth(handle).image(ImagePath,true,true,400,0,null,0,0.0f);
Usman Kurd
  • 7,212
  • 7
  • 57
  • 86
0
loadImage("http://relinjose.com/directory/filename.png");

Here you go

void loadImage(String image_location) {
    URL imageURL = null;
    if (image_location != null) {
        try {
            imageURL = new URL(image_location);         
            HttpURLConnection connection = (HttpURLConnection) imageURL
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream inputStream = connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);// Convert to bitmap
            ivdpfirst.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        //set any default
    }
}
Exceptional
  • 2,994
  • 1
  • 18
  • 25
0

You may try the well written UrlImageViewHelper library:

https://github.com/koush/UrlImageViewHelper

The usage is simple:

UrlImageViewHelper.setUrlDrawable(imageView, "http://example.com/image.png");
Mohsen Afshin
  • 13,273
  • 10
  • 65
  • 90