【问题标题】:I cant display my downloaded images on my app我无法在我的应用程序上显示我下载的图像
【发布时间】:2020-06-07 23:37:37
【问题描述】:

在更新状态方法中:

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】:

所以 asynctask 实例输出一串数据,然后你把它作为一个 Bitmap 并希望它设置为一个 Imageview 的源?那是不对的。

首先将数据放入一个字节数组中(这将是 asynctask 类的输出)。然后把它变成Bitmap:

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));

【讨论】:

  • 让我试试这个然后回复你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多