我无法在我的应用程序上显示下载的图像

问题描述 投票:0回答:1

在更新状态方法中:

public void updateStatus() {

    Random random = new Random();
    URLSize = celebURLs.size();

    if (URLSize <= 0) {
        URLSize++;
        //chosenImage = random.nextInt(URLSize);
    } else {
        chosenImage = random.nextInt(URLSize);
    }

    ImageDownloader imageDownloader = new ImageDownloader();
    Bitmap celebPics;

    try {
        celebPics = imageDownloader.execute(celebURLs.get(chosenImage)).get();
        imageView.setImageBitmap(celebPics);

        locationOfCorrectAnswer = random.nextInt(4);
        int incorrectAnswer;
        for (int i = 0; i < 4; i++) {
            if (i == locationOfCorrectAnswer) {
                answers[i] = chosenNames.get(chosenImage);
            } else {
                incorrectAnswer = random.nextInt(celebURLs.size());
                while (incorrectAnswer == chosenImage) {
                    incorrectAnswer = random.nextInt(celebURLs.size());
                }
                answers[i] = chosenNames.get(incorrectAnswer);
            }

        }
        button1.setText(String.format("%s", answers[0]));
        button2.setText(String.format("%s", answers[1]));
        button3.setText(String.format("%s", answers[2]));
        button4.setText(String.format("%s", answers[3]));

    } catch (Exception e) {
        e.printStackTrace();
    }

}

我更改了代码,因为它带来了一个异常“ IllegealArgumentException n必须为正”,但是即使在更改代码以始终增加数组列表的随机生成值的大小之后,它仍然无法正常工作

java android arrays random bitmap
1个回答
0
投票

因此asynctask实例输出一串数据,然后将其作为位图并希望将其设置为Imageview的源?那是不对的。

首先将数据放入字节数组中(这将是asynctask类的输出)。然后将其转换为位图:

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(Bitmap.createScaledBitmap(bmp, image.getWidth(),
                image.getHeight(), false));
© www.soinside.com 2019 - 2024. All rights reserved.