团队博客

图片在客户端的预览出了问题,我觉得是图片地址的获取出了问题。

看了些博客,简单敲了些代码针对想法做了个测试。

弄了个小小的个人模板:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.xiaoyu.schoolelive.R;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class testActivity extends AppCompatActivity {
    private String str;
    private MyHandler handler1;
    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            ImageView imageView = (ImageView)findViewById(R.id.image);
            imageView.setImageBitmap((Bitmap)msg.obj);
        }
    }

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test2);
        handler1 = new MyHandler();
        Button button = (Button) findViewById(R.id.test);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        try {
                            String path = "这里是图片地址";
                            URL url = new URL(path);
                            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                            conn.setRequestMethod("GET");
                            conn.setConnectTimeout(8000);
                            conn.setReadTimeout(8000);
                            conn.connect();
                            //如果响应码为200,说明请求成功
                            if(conn.getResponseCode() == 200)
                            {
                                InputStream is = conn.getInputStream();
                                Bitmap bm = BitmapFactory.decodeStream(is);
                                Message msg = new Message();
                                msg.obj = bm;
                                handler1.sendMessage(msg);
                                handler1.sendMessage(msg);
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            }
        });
    }
}

 

posted @ 2021-05-04 22:58  谦寻  阅读(593)  评论(0编辑  收藏  举报