Android的 - 正确的多线程 [英] Android - Correct Multithreading

查看:177
本文介绍了Android的 - 正确的多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人可以给我一个手与此图像下载code?我希望它在后台运行,但它似乎是新的主题(新的Runnable())绝对不是要走的路,根据机器人的文档,我不知道怎么回事接近这样的:

  //来电
而(exhibitorCursor.moveToNext())
{
  新主题(新的Runnable()
  {
    公共无效的run()
    {
      downloadImage(exhibitorId,exhibitorString,DOWNLOAD_EXHIBITOR);
    }
  })。开始();
}

//第一个函数
公共无效downloadImage(长ID,字符串externalImageUrl,整型)
{
  //逻辑这里的垃圾

  如果(!(新文件(localImageName).exists()))
  {
    DownloadFromUrl(externalImageUrl,localImageName);
  }
}

//第二个功能
公共无效DownloadFromUrl(字符串fileUrl,字符串文件名)
{
  //这是下载方法
  尝试
  {
    网址URL =新的URL(fileUrl);

    档案文件=新的文件(文件名);

    URLConnection的UCON = url.openConnection();

    InputStream的是= ucon.getInputStream();
    的BufferedInputStream双=新的BufferedInputStream(是,8192);

    ByteArrayBuffer BAF =新ByteArrayBuffer(50);
    INT电流= 0;
    而((电流= bis.read())!=  -  1)
    {
      baf.append((字节)电流);
    }

    / *读取转换为字符串的字节数。 * /
    FileOutputStream中FOS =新的FileOutputStream(文件);
    fos.write(baf.toByteArray());
    fos.close();
  }
  赶上(IOException异常E)
  {
    Log.d(ImageManager,错误:+ E);
  }

}
 

是否有这样做的不太痛苦的样子?我只是下载像20的图像在应用程序后使用,它被锁起来的时候了。

这可能是不相关的,但是这是我如何的OBJ-C实现它的iPhone版本。

 的(NSDictionary中*参展商在参展商)
{
    [自performSelectorInBackground:@selector(downloadExhibitorImage :) withObject:展商]
}
 

解决方案

看看的的下载管理,并于替代 AsyncTask的

Can someone please give me a hand with this image downloading code? I want it to run in the background, but it seems like new Thread(new Runnable()) is definitely not the way to go, according to the Android docs, and I'm not sure how else to approach this:

// caller
while( exhibitorCursor.moveToNext() )
{
  new Thread(new Runnable()
  {
    public void run()
    {
      downloadImage(exhibitorId, exhibitorString, DOWNLOAD_EXHIBITOR);
    }
  }).start();
}

// first function
public void downloadImage(long id, String externalImageUrl, int type)
{
  // logic junk here

  if( !(new File(localImageName).exists()) )
  {
    DownloadFromUrl(externalImageUrl, localImageName);
  }
}

// second function
public void DownloadFromUrl(String fileUrl, String fileName)
{
  // this is the downloader method
  try
  {
    URL url = new URL(fileUrl);

    File file = new File(fileName);

    URLConnection ucon = url.openConnection();

    InputStream is = ucon.getInputStream();
    BufferedInputStream bis = new BufferedInputStream(is, 8192);

    ByteArrayBuffer baf = new ByteArrayBuffer(50);
    int current = 0;
    while( (current = bis.read()) != -1 )
    {
      baf.append((byte)current);
    }

    /* Convert the Bytes read to a String. */
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(baf.toByteArray());
    fos.close();
  }
  catch( IOException e )
  {
    Log.d("ImageManager", "Error: " + e);
  }

}

Is there a less painful way of doing this? I'm only downloading like 20 images to use later in the app, and it is locking it up right away.

It may not be relevant, but this is how I am achieving it in Obj-C for the iPhone version.

for( NSDictionary *exhibitor in exhibitors )
{
    [self performSelectorInBackground:@selector(downloadExhibitorImage:) withObject:exhibitor];
}

解决方案

Take a look at the DownloadManager and as an alternative at AsyncTask

这篇关于Android的 - 正确的多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆