gpt4 book ai didi

java - 使用 AsyncTask 下载许多图像并将它们发布到 ImageView

转载 作者:太空宇宙 更新时间:2023-11-03 12:56:25 24 4
gpt4 key购买 nike

我坐下来尝试用 Android 做一些练习。我今天的重点是制作简单的应用程序,它将下载数据(来自 URL 的图像)并将它们显示在布局中的 ImageView 控件中。我在网上看到了一些示例并完成了我的应用程序。一切似乎都很好,但当我按下按钮时,我开始了它的工作,但随后无法显示错误:NULL POINTER 9error reading file)。这是我的代码:

package com.example.htmlcontent;

import java.io.BufferedInputStream;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;

import android.app.Activity;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


public class MainActivity extends Activity {
private ImageView mImageView;
private ImageView mImageView2;
public Button button;
public static ArrayList<Drawable> drawable;

public static String[] URLs = {"http://zitterman.com/wp-content/uploads/2013/07/19194927_1371972212.jpg","http://i.imgur.com/CQzlM.jpg"};

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


mImageView = (ImageView) findViewById(R.id.test_image);
mImageView2 = (ImageView) findViewById(R.id.test_image2);
button = (Button) findViewById(R.id.download1);

button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

new DownloadImage().execute();
}
});
}


/**
* Simple functin to set a Drawable to the image View
* @param drawable
*/
@SuppressWarnings("deprecation")
private void setImage()
{
if(drawable.get(0) == null)
{
System.out.println("DRAWABLE JEST NULL");
}
mImageView.setBackgroundDrawable(drawable.get(0));
mImageView2.setBackgroundDrawable(drawable.get(1));
}

public class DownloadImage extends AsyncTask<Void, Void, Void> {



/**
* Called after the image has been downloaded
* -> this calls a function on the main thread again
*/
protected void onPostExecute(Drawable image)
{
setImage();
}
protected void onPreExecute()
{
Log.i("333333", "Uruchamiam WATEK SCIAGANIA ASYNCTASKIEM PLIKU Z NETA");
}

@Override
protected Void doInBackground(Void... params) {

downloadImage();
return null;
}
/**
* Actually download the Image from the _url
* @param _url
* @return
*/
@SuppressWarnings("deprecation")
private void downloadImage()
{
//Prepare to download image

URL url;

InputStream in;
BufferedInputStream buf;

//BufferedInputStream buf;
for(int i = 0; i<URLs.length; i++)
{
try {
url = new URL(URLs[i]);
in = url.openStream();

// Read the inputstream
buf = new BufferedInputStream(in);

// Convert the BufferedInputStream to a Bitmap
Bitmap bMap = BitmapFactory.decodeStream(buf);
if (in != null) {
in.close();
}
if (buf != null) {
buf.close();
}

drawable.add(new BitmapDrawable(bMap));

} catch (Exception e) {
Log.e("Error reading file", e.toString());
}

}

}


}
}

和我的 XML 文件布局:

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

<Button
android:id="@+id/download1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />

<TextView
android:layout_width="102dp"
android:layout_height="wrap_content"
android:text="hello" />

<ImageView
android:id="@+id/test_image"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/ic_launcher" />
<ImageView
android:id="@+id/test_image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher" />

</LinearLayout>

正如您在代码 ArrayList 中看到的那样,我制作了可绘制列表。代码没有错误。只有 NULL POINTER。

最佳答案

我认为这是因为您忘记了初始化可绘制对象。更改为:

public static ArrayList<Drawable> drawable = new ArrayList<Drawable>();

下一步,因为你的 AsyncTask 是 <Void, Void, Void> .您的帖子执行应如下所示:

    @Override
protected void onPostExecute(Void aVoid) {
setImage();
}

泛型类型 <A,B,C>对应不同方法的参数和返回类型。您应该在这里阅读更多相关信息:https://stackoverflow.com/a/6053673/827110

(为了完整起见)您还需要在您的 AndroidManifest.xml 中获得互联网许可添加(就在 <application.. 之前):

<uses-permission android:name="android.permission.INTERNET" />

关于java - 使用 AsyncTask 下载许多图像并将它们发布到 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19452590/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com