Android 我没有使用此代码从url获取图像

Android 我没有使用此代码从url获取图像,android,Android,我正在使用以下代码 public class Img extends Activity { /** Called when the activity is first created. */ ImageView img; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);

我正在使用以下代码

 public class Img extends Activity {
/** Called when the activity is first created. */
ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    img = (ImageView)findViewById(R.id.imgview);

    Bitmap bm = getBitmapFromURL("http://l.yimg.com/a/i/us/we/52/21.gif");
    if(bm == null)
        Toast.makeText(this, "Image can't load", 1).show();
    else
        img.setImageBitmap(bm);
}


public static Bitmap getBitmapFromURL(String src) {
    try {
        Log.e("src",src);
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        Log.e("Bitmap","returned");
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        Log.e("Exception",e.getMessage());
        return null;
    }
}
}

我收到一条消息“图像无法加载”

请尝试下面的代码

 ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    img = (ImageView)findViewById(R.id.imgview);

    Drawable image = getBitmapFromURL("http://l.yimg.com/a/i/us/we/52/21.gif");
    if(image == null)
        Toast.makeText(this, "Image can't load", 1).show();
    else
        img.setImageDrawable(image);
}

public Drawable  getBitmapFromURL(String url) {
try {
    InputStream is = (InputStream) new URL(url).getContent();
    Drawable d = Drawable.createFromStream(is, "src name");
    return d;
} catch (Exception e) {
    return null;
}
}请尝试下面的代码

 ImageView img;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    img = (ImageView)findViewById(R.id.imgview);

    Drawable image = getBitmapFromURL("http://l.yimg.com/a/i/us/we/52/21.gif");
    if(image == null)
        Toast.makeText(this, "Image can't load", 1).show();
    else
        img.setImageDrawable(image);
}

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

private Bitmap downloadUrl(String url) {
                InputStream myInputStream =null;
             Bitmap myBitmap;
        StringBuilder sb = new StringBuilder();
                //adding some data to send along with the request to the server
        sb.append("name=Anthony");
        URL url;
        try {
            url = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            OutputStreamWriter wr = new OutputStreamWriter(conn
                    .getOutputStream());
                        // this is were we're adding post data to the request
                        wr.write(sb.toString());
            wr.flush();
            myInputStream = conn.getInputStream();
            wr.close();
                     myBitmap = BitmapFactory.decodeStream(myInputStream);

        } catch (Exception e) {
                        //handle the exception !
            Log.d(TAG,e.getMessage());
        }
                return myBitmap;
}
试试这个

private Bitmap downloadUrl(String url) {
                InputStream myInputStream =null;
             Bitmap myBitmap;
        StringBuilder sb = new StringBuilder();
                //adding some data to send along with the request to the server
        sb.append("name=Anthony");
        URL url;
        try {
            url = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setDoOutput(true);
            conn.setRequestMethod("POST");
            OutputStreamWriter wr = new OutputStreamWriter(conn
                    .getOutputStream());
                        // this is were we're adding post data to the request
                        wr.write(sb.toString());
            wr.flush();
            myInputStream = conn.getInputStream();
            wr.close();
                     myBitmap = BitmapFactory.decodeStream(myInputStream);

        } catch (Exception e) {
                        //handle the exception !
            Log.d(TAG,e.getMessage());
        }
                return myBitmap;
}