空白应用程序,不显示任何错误

时间:2018-08-08 13:11:22

标签: android

这是我的图像下载应用程序,它显示从图像URL下载的图像并将其显示在listView中。 该应用程序运行正常且未显示任何错误,但显示为空白,我看不到任何东西。

ImageDownloader:

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {

    private Bitmap bitmap;

    public Bitmap getBitmap() {
        return bitmap;
    }

    @Override
    protected Bitmap doInBackground(String... params) {

        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();
            if (connection.getContentType().contains("image")) {
                InputStream inputStream = connection.getInputStream();
                bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

RowItem:

public class RowItem {

    private Bitmap bitmap;

    public RowItem(Bitmap bitmap) {
        this.bitmap = bitmap;
    }

    public Bitmap getBitmap() {
        return bitmap;
    }
}

CustomListViewAdapter:

public class CustomListViewAdapter extends ArrayAdapter<RowItem> {

    Context context;

    public CustomListViewAdapter(Context context, int resourceId, List<RowItem> items) {
        super(context, resourceId, items);
        this.context = context;
    }

    private class ViewHolder {
        ImageView imageView;
    }

    public View getView(int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;

        RowItem rowItem = getItem(position);

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);

        if (convertView == null) {

            convertView = inflater.inflate(R.layout.list_item, null);

            holder = new ViewHolder();

            holder.imageView = (ImageView) convertView.findViewById(R.id.imageView);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.imageView.setImageBitmap(rowItem.getBitmap());

        return convertView;
    }

}

MainActivity:

public class MainActivity extends AppCompatActivity {

    ArrayList<String> urls = new ArrayList<>();

    ArrayList<Bitmap> bitmaps = new ArrayList<>();

    ListView listView;

    List<RowItem> rowItems;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println("Flag 1");
        addUrls();
        System.out.println("Flag 2");
        for (int i = 0; i < urls.size(); i++) {

            ImageDownloader imageDownloader = new ImageDownloader();

            imageDownloader.execute(urls.get(i));

            bitmaps.add(imageDownloader.getBitmap());

            System.out.println("Size"+bitmaps.size());
        }

        System.out.println("Flag 3");
        rowItems = new ArrayList<>();

        for (int i = 0; i < bitmaps.size(); i++) {
            RowItem item = new RowItem(bitmaps.get(i));
            rowItems.add(item);
        }
        System.out.println("Flag 4");
        listView = (ListView) findViewById(R.id.list);
        CustomListViewAdapter adapter = new CustomListViewAdapter(this, R.layout.list_item, rowItems);
        listView.setAdapter(adapter);
        System.out.println("Flag 5");
    }

    public void addUrls() {

        urls.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTXEZRoYOhIJxL5foNz_NlatDlgYStzZgVIiKuo6vtRtz2wY-8b4Q");
        urls.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSFL3WYbqNOX-dwjtT1LroBlY5W-3YuwSIuCMRaLpnjMXbVPEJy");
        urls.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQiwgrJeAJN-7lcy92N51uP7XzccK_p-fTSJNCXPLPSVih8wqPf");
        urls.add("https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT19dYLCEZlMRqojedJB-05jTrflD74nasvkXs-SdVeyM2BEpCSFA");
        urls.add("http://wallpaperswide.com/download/high_tech_earth-wallpaper-2880x1800.jpg");
        urls.add("https://www.gettyimages.ca/gi-resources/images/Homepage/Hero/UK/CMS_Creative_164657191_Kingfisher.jpg");
        urls.add("https://images.unsplash.com/photo-1418489098061-ce87b5dc3aee?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=2f033882f3c25404e3f904fbfe2351be&w=1000&q=80");
        urls.add("https://techcrunch.com/wp-content/uploads/2018/03/gettyimages-705351545.jpg?w=730&crop=1");

    }
}

ativity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="com.example.danishrizvi.test4.MainActivity">

    <ListView
        android:id="@+id/list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

list_item.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:visibility="visible"/>

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

我建议您使用一个库从PicassoGlide之类的URL加载图像(使用内存和磁盘缓存以避免每次都下载同一图像)

答案 1 :(得分:1)

棘手的部分是这些行:

imageDownloader.execute(urls.get(i));
bitmaps.add(imageDownloader.getBitmap());

由于ImageDownloaderAsyncTask,因此它在另一个线程上以异步方式执行,并且到第二行被调用时,仍未收到任何消息。您真正需要做的是覆盖AsyncTask#onPostExecute,您将在其中接收请求结果(位图)并将其推送到适配器。