在android中通过URL使用imageview

在android中通过URL使用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

最佳答案

试试这个简单的方法:

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


它的在线获取,因此请检查您的Internet权限:

添加权限:

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

关于android - 在android中通过URL使用imageview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17986223/

10-09 01:30