2

我尝试从 url 获取照片,但我无法获取照片。

当前代码:

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
4

6 回答 6

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" />
于 2013-08-01T06:26:31.863 回答
1

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

使用AsynckTask'onPostExecute方法下载图像位图。ImageView将您的对象与返回的位图绑定。

于 2013-08-01T06:15:42.243 回答
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
    }
}
于 2013-08-01T06:23:13.477 回答
0

您可以尝试编写良好的UrlImageViewHelper库:

https://github.com/koush/UrlImageViewHelper

用法很简单:

UrlImageViewHelper.setUrlDrawable(imageView, "http://example.com/image.png");
于 2013-08-01T06:25:22.577 回答
0

从 URL 加载 Android 图像的最佳教程:

尝试:此代码将对您有所帮助

于 2013-08-01T06:06:16.527 回答
0

关于加载图像有以下几种可能

1- 图像受基本身份验证保护

2- 网址无效

第一次更改 URL 以确认您的操作是完美的,您将看到图像。

在第二种情况下,我建议您使用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);
于 2013-08-01T06:18:21.737 回答