wuhudamahua 2016-08-09 17:03 采纳率: 0%
浏览 1052

简单的Android网路图片查看器,可是总是显示不了图片

网络权限已经添加了,就是运行起来点击按钮没反应
这是java代码
package com.example.activity;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity{
private Button bt;
private ImageView iv;
Handler handler=new Handler();
public void handleMessage(Message msg){
if(msg.what==1){
Bitmap bt=(Bitmap) msg.obj;
iv.setImageBitmap(bt);
Toast.makeText(MainActivity.this, "1", Toast.LENGTH_SHORT).show();
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv=(ImageView) findViewById(R.id.iv);
bt=(Button) findViewById(R.id.bt);

bt.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            new Thread(new Runnable()
            {
                @Override
                public void run() {
                    String path="http://www.sinaimg.cn/dy/slidenews/1_img/2016_32/2841_720360_233449.jpg";
                    try {
                        URL url=new URL(path);
                        HttpURLConnection conn=(HttpURLConnection) url.openConnection();
                        conn.setRequestMethod("GET");
                        conn.setConnectTimeout(5000);
                        conn.setReadTimeout(5000);
                        conn.connect();
                        InputStream is=conn.getInputStream();
                        Bitmap b=BitmapFactory.decodeStream(is);
                        Message msg=new Message();
                        msg.obj=b;
                        msg.what=1;
                        handler.sendMessage(msg);
                    } catch (MalformedURLException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }

    });         
}

}
这是xml
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<Button
    android:id="@+id/bt"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ImageView
    android:id="@+id/iv"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

  • 写回答

1条回答 默认 最新

  • Garnet-F 2016-08-10 00:19
    关注

    你应该在监听按钮事件里面开始线程

    评论

报告相同问题?

悬赏问题

  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型