zz670384114 2016-04-21 11:28 采纳率: 100%
浏览 1697
已采纳

安卓,通过url获取图片动态生成ImageView为什么生成不了?可能是什么原因?

package com.example.zz670.mypic;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    new work().start();
}

private class work extends Thread {
    InputStream inputStream = null;
    HttpURLConnection urlConnection = null;
    String response;
    String[] gURL = new String[10];
    Bitmap bitmap;
    ImageView imageView;
    LinearLayout linearLayout;
    public void run() {
        try {
            URL url = new URL("http://gank.io/api/random/data/福利/1");
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setConnectTimeout(5000);
            urlConnection.setRequestMethod("GET");
            int statusCode = urlConnection.getResponseCode();
            if (statusCode == 200) {
                inputStream = new BufferedInputStream(urlConnection.getInputStream());
                response = inputStream2String(inputStream);

            }
            if (!response.isEmpty()) {
                try {
                    JSONObject jsonObject1 = new JSONObject(response);
                    // Log.e("Json", response);
                    JSONArray jsonArray = jsonObject1.getJSONArray("results");
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = (JSONObject) jsonArray.get(i);
                        //取出name
                        String jarray1 = jsonObject.getString("url");

                        for (int j = 0; j < 10; j++) {
                            gURL[j] = jarray1;
                            Log.e("Json", jarray1.toString());
                            // Log.e("Json", gURL[j]);
                        }
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        linearLayout = (LinearLayout) findViewById(R.id.id_gallery);
        ImageView[] imageViews = new ImageView[10];
        for (int i = 0; i < imageViews.length; i++) {

            imageViews[i] = imageView = new ImageView(MainActivity.this);
            try {
                Bitmap bitmap = getBitmap(gURL[i]);
                imageView.setImageBitmap(bitmap);
                linearLayout.addView(imageView);
            } catch (Exception e) {
            }
        }
    }

    String inputStream2String(InputStream is) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(is));
        StringBuffer buffer = new StringBuffer();
        String line = "";
        while ((line = in.readLine()) != null) {
            buffer.append(line);
        }
        return buffer.toString();
    }

    private Bitmap getBitmap(String path) throws IOException {

        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if (conn.getResponseCode() == 200) {
             inputStream = conn.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
            return bitmap;
        }
        return null;
    }
}

}


  • 写回答

3条回答 默认 最新

  • 恋恋西风 2016-04-22 06:35
    关注

    你是在线程里执行的 请求URL 路径,是异步的,
    这里会先执行:
    for (int i = 0; i < imageViews.length; i++) {

            imageViews[i] = imageView = new ImageView(MainActivity.this);
            try {
                Bitmap bitmap = getBitmap(gURL[i]);
                imageView.setImageBitmap(bitmap);
                linearLayout.addView(imageView);
            } catch (Exception e) {
            }
        }
    

    所有 gURL[i]全是空,你可输出Log看一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 鼠标右键,撤销删除 复制 移动,要怎样删除
  • ¥15 使用MATLAB进行余弦相似度计算加速
  • ¥15 服务器安装php5.6版本
  • ¥15 我想用51单片机和数码管做一个从0开始的计数表 我写了一串代码 但是放到单片机里面数码管只闪烁一下然后熄灭
  • ¥20 系统工程中,状态空间模型中状态方程的应用。请猛男来完整讲一下下面所有问题
  • ¥15 我想在WPF的Model Code中获取ViewModel Code中的一个参数
  • ¥15 arcgis处理土地利用道路 建筑 林地分类
  • ¥20 使用visual studio 工具用C++语音,调用openslsx库读取excel文件的sheet问题
  • ¥100 寻会做云闪付tn转h5支付链接的技术
  • ¥15 DockerSwarm跨节点无法访问问题