使用asynctasks在Android中刷新多个imageView?

时间:2012-01-17 10:00:41

标签: android multithreading imageview multimedia

我要做的是在一个活动上播放视频,视频的数量是未定义的,可以是1到8,在我的情况下,视频是一个图像序列,其中每个图像都是从凸轮上下载的使用固定时间的互联网。

单个视频活动不是问题,我可以使用ImageView和AsyncTask,使用此方法的许多实例,当我尝试使多个视频活动不起作用时,只播放一个视频。我不确切知道它发生了什么,但我认为由于UIThread,它可能是一个与功能相关的问题。

这里使用的AsyncTask代码:

private class AsyncTask_LiveView extends AsyncTask<String, Integer, Void> 
{   
    private String sImageMessage = "";

    private final WeakReference<ImageView> imageViewReference;
    private Bitmap bmImage = null;
    private String url = "";
    private String usr = "";
    private String pwd = "";
    private utils u = new utils();

    public AsyncTask_LiveView(ImageView imageView, String Url, String Usr, String Pwd)
    {
        imageViewReference = new WeakReference<ImageView>(imageView);
        url = Url;
        usr = Usr;
        pwd = Pwd;
    }

    // automatically done on worker thread (separate from UI thread)
    @Override
    protected Void doInBackground(final String... args) 
    {
        while(!isCancelled())
        {
            if(isCancelled())
                 return null;

            SystemClock.sleep(200);

            Log.v("ImageDownload","test");
            bmImage = u.DownloadBitmapFromUrl(url, usr, pwd);

            publishProgress(0);
        }

        return null;
    }

    // can use UI thread here
    @Override
    public void onProgressUpdate(Integer... i)
    {
        Log.v("Image", "Setup Image");
        if (imageViewReference != null) {
            ImageView imageView = imageViewReference.get();
            if (imageView != null) {
                imageView.setImageBitmap(bmImage);
            }
        }
    }
 }

我以这种方式启动AsyncTasks:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layoutliveviewdouble);

        this.imgV1 = (ImageView ) findViewById(R.id.imageView1);
    aTaskImgV1 = new AsyncTask_LiveView(imgV1,
                                        URL1,
                                        "",
                                        "");


    this.imgV2 = (ImageView ) findViewById(R.id.imageView2);
    aTaskImgV2 = new AsyncTask_LiveView(imgV2,
                                        URL2,
                                        "root",
                                        "jenimex123");

    aTaskImgV1.execute();
    aTaskImgV2.execute();
}

DownloadBitmapFromUrl方法是:

public Bitmap DownloadBitmapFromUrl(String imageURL, final String usr, final String pwd) {  //this is the downloader method
    try {
        URL url = new URL(imageURL); 


        /* Open a connection to that URL. */
        HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
        ucon.setRequestMethod("GET");
        ucon.setDoOutput(true);
        Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication (usr, pwd.toCharArray());
            }
        });
        ucon.connect();

        /*
         * Define InputStreams to read from the URLConnection.
         */
        InputStream is =  ucon.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);

        /*
         * Read bytes to the Buffer until there is nothing more to read(-1).
         */
        ByteArrayBuffer baf = new ByteArrayBuffer(100000);
        int current = 0;
        while ((current = bis.read()) != -1) {
                baf.append((byte) current);
        }

        /* Convert the Bytes read to a String. */
        Bitmap bmp = BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.length());

        return bmp;
    } catch (Exception e) {
            //Log.d("ImageManager", "Error: " + e);
        return null;
    }
}

任何想法?

解决方案:(2011年1月21日)

线条:

Authenticator.setDefault (new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication (usr, pwd.toCharArray());
            }
        });

制动机构。事实上,只有一个凭证对可以设置为全局,而其他下载流程则在请求使用错误的凭据时停留。

解决方案是:

String authString = usr + ":" + pwd;
byte[] authEncBytes = Base64.encode(authString.getBytes(), Base64.DEFAULT);
String authStringEnc = new String(authEncBytes);
ucon = (HttpURLConnection) url.openConnection();

if(_usr != "")
   ucon.setRequestProperty("Authorization", "Basic " + authStringEnc);

感谢所有人。

3 个答案:

答案 0 :(得分:0)

我认为您应该为每个AsyncTask_LiveView使用唯一的ImageView

答案 1 :(得分:0)

这个函数是否支持多个线程?

bmImage = u.DownloadBitmapFromUrl(url, usr, pwd);

答案 2 :(得分:0)

看起来你在downloadBitmapFromUrl(..)方法中调用的一些方法涉及一些常见对象的同步。尝试向此方法的每个部分添加一些额外的日志记录,并查看每个线程卡在哪里。我会这样做:

public Bitmap downloadBitmapFromUrl(String imageURL, final String usr, final String pwd) {  //this is the downloader method
    try {
    ...
    Log.i(toString() + " in " + Thread.currentThread(), "is about to open connection...");
    HttpURLConnection ucon = (HttpURLConnection) url.openConnection();
    Log.i(toString() + " in " + Thread.currentThread(), "has opened connection");

    ...

等等。