2 slaks SLaks 于 2013.05.13 15:54 提问

从远程服务器获取图像问题

从远程服务器获取图像是一个好的简单的方法吗?

当我试着在 android AVD 实现这项功能时,在屏幕上和 log_cat上什么也没显示,没有错误也没有崩溃。如何解决这个问题呢?

public class MainActivity extends Activity {
Bitmap bitmap = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try{
        bitmap = getBitmap("https://twimg0-a.akamaihd.net/profile_images/2275552571/image_normal.jpg");
        ImageView img = (ImageView) findViewById(R.id.img);
        img.setImageBitmap(bitmap);
    }catch(Exception e){
    }
}
public Bitmap getBitmap(String bitmapUrl) { 
              try {
                URL url = new URL(bitmapUrl);
                return BitmapFactory.decodeStream(url.openConnection().getInputStream());
              }catch(Exception ex) {return null;}
            }
 }

3个回答

hxn_217
hxn_217   2013.05.14 15:37
已采纳

你可以同步的,或者异步的下载远程图像:

ImageDownloader imageDownloader = new ImageDownloader();
    imageDownloader.setMode(ImageDownloader.Mode.CORRECT);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.picture);

    imageView.setLayoutParams(new GridView.LayoutParams(140, 140));
    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
    imageView.setPadding(4, 4, 4, 4);

    List<ImageSize> images = this.pictures.get(position).getImages();

    imageDownloader.download(images.get(images.size()-3).getSource(), imageView);

请参考:http://code.google.com/p/android-imagedownloader/

luhuajcdd
luhuajcdd   2013.05.14 12:56
private Drawable LoadImageFromWebOperations(String url) {
try {
    InputStream is = (InputStream) new URL(url).getContent();
    Drawable d = Drawable.createFromStream(is, "image.png");
    return d;
    }
catch (Exception e) 
{
    System.out.println("Exc=" + e);
    return null;
}
}
franzhong
franzhong   2013.05.14 13:10

这样再试试呢

public class MainActivity extends Activity {
    URL url=null;
    String hsUrl = "https://twimg0-a.akamaihd.net/profile_images/2275552571/image_normal.jpg";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView bm = (ImageView) findViewById(R.id.mIV);
        try {
            url = new URL(hsUrl);
            HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
            X509TrustManager xtm = new X509TrustManager() {
                    @Override
                    public X509Certificate[] getAcceptedIssuers() {
                            // TODO Auto-generated method stub
                            return null;
                    }
                    @Override
                    public void checkServerTrusted(X509Certificate[] arg0,
                                    String arg1) throws CertificateException {
                            // TODO Auto-generated method stub
                    }
                    @Override
                    public void checkClientTrusted(X509Certificate[] arg0,
                                    String arg1) throws CertificateException {
                            // TODO Auto-generated method stub
                    }
            };
            TrustManager[] tm = { xtm };
            SSLContext ctx = SSLContext.getInstance("TLS");
            ctx.init(null, tm, null);
            con.setSSLSocketFactory(ctx.getSocketFactory());
            con.setHostnameVerifier(new HostnameVerifier() {
                    @Override
                    public boolean verify(String arg0, SSLSession arg1) {
                            return true;
                    }
            });
            InputStream ss = con.getInputStream();
            Bitmap bm1 = BitmapFactory.decodeStream(ss);
            bm.setImageBitmap(bm1);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
    }

}

manifest权限

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

没看到,一楼的方法也可以,以上是https访问方式

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!