在android中使用url的imageview

时间:2013-08-01 05:59:46

标签: android inputstream android-imageview

我尝试从网址上获取照片但我无法获取照片。

当前代码:

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;
    }
}

这就是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

6 个答案:

答案 0 :(得分:1)

你做错了是在主UI线程上调用长时间运行的任务。

对下载的图像位图使用AsynckTask的{​​{1}}方法。使用返回的位图绑定onPostExecute对象。

答案 1 :(得分:1)

试试这个简单的:

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;
    }
}

在线获取检查您的互联网许可:

添加权限:

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

答案 2 :(得分:0)

从网址加载Android图片的最佳教程:

尝试:this code will be helpful to you

答案 3 :(得分:0)

加载图像有以下可能性

1-图像受基本身份验证

保护

2-网址无效

首先更改网址以确认您的操作是完美的,然后您会看到图片。

在第二种情况下,我建议您使用AQUERY (Android Query) 下载最新的jar将它添加到您的项目中并以这种方式使用,通过基本身份验证

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);

答案 4 :(得分:0)

loadImage("http://relinjose.com/directory/filename.png");

你去吧

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
    }
}

答案 5 :(得分:0)

您可以尝试写得很好的UrlImageViewHelper库:

https://github.com/koush/UrlImageViewHelper

用法很简单:

UrlImageViewHelper.setUrlDrawable(imageView, "http://example.com/image.png");