SLaks
2013-05-13 07:54
采纳率: 60.5%
浏览 2.2k

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

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

当我试着在 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 2013-05-14 07: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 2013-05-14 04: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 2013-05-14 05: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访问方式

    打赏 评论

相关推荐 更多相似问题