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

使用asynctasks在Android中刷新多个imageView?,android,multithreading,imageview,multimedia,Android,Multithreading,Imageview,Multimedia,我要做的是在单个活动上播放视频,视频的数量没有定义,可以是1到8,在我的例子中,视频是一个图像序列,其中每个图像都是使用固定时间间隔从互联网上的cam下载的 做一个单一的视频活动不是问题,我可以使用ImageView和AsyncTask来做,当我尝试制作多个视频活动时,使用这种方法的许多实例都不起作用,只有一个视频播放。我不知道到底发生了什么,但我认为这可能是一个货币相关的问题,由于UIThread 下面是使用的异步任务代码: private class AsyncTask_LiveView e

我要做的是在单个活动上播放视频,视频的数量没有定义,可以是1到8,在我的例子中,视频是一个图像序列,其中每个图像都是使用固定时间间隔从互联网上的cam下载的

做一个单一的视频活动不是问题,我可以使用ImageView和AsyncTask来做,当我尝试制作多个视频活动时,使用这种方法的许多实例都不起作用,只有一个视频播放。我不知道到底发生了什么,但我认为这可能是一个货币相关的问题,由于UIThread

下面是使用的异步任务代码:

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);
            }
        }
    }
 }
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());
            }
        });
我们正在制动机械装置。事实上,只能全局设置一个凭据对a,而其他下载进程在使用错误的凭据进行请求时遇到了问题

解决办法是:

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

感谢大家。

我认为您应该为每个ImageView使用唯一的AsyncTask\u LiveView。

此函数支持多线程吗

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

看起来像是在downloadBitmapFromUrl中调用的一些方法。。方法涉及一些公共对象的同步。尝试向该方法的每个部分添加一些额外的日志记录,看看每个线程在哪里被卡住。我会这样做:

    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();
}
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");

    ...

等等。

你是如何开始这些任务的?你能给我看一下被卡住的地方的日志吗?15-20行?我周四之前都不上班。我会在我被困的那一刻写日志。感谢您愿意帮助我。对象u在每个AsyncTask中都是一个新实例,所以我认为它是。结果表明,我只测试2视频流案例的另一个被阻止的AsyncTask在current=bis.read!=-再一次{baf.appendbyte current;}:有什么想法吗?提前谢谢。