我想在页面中下载一个连接的图片,把下载的代码放到了service 中。而下载时在activity binding service .
代码如下:final Intent service = new Intent();
service.setAction("com.hkrt.acttion.DOWNLOAD_BINDLE");
bindService(service, connection, Service.BIND_AUTO_CREATE);
Thread thread = new Thread(new DownloadThread ());
thread.start();在DownloadThread  调用bind 的方法。 class DownloadThread  implements Runnable{
@Override
public void run() {
String imageSuffix = imagePath.substring(imagePath.lastIndexOf("."), imagePath.length());
boolean  down = downLoadBinder.getService().downlaodFile(imagePath,ImageService.getDateTime()+imageSuffix );
Message message = new Message();
Bundle bun = new Bundle();
bun.putBoolean("downLoadState", down);
message.setData(bun);
downLoadHandler.sendMessage(message);


}
  
  }绑定如下:    private  DownLoadBinder downLoadBinder;
    
    private  ServiceConnection connection =  new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder binder) {
downLoadBinder = (DownLoadBinder)binder;

}
@Override
public void onServiceDisconnected(ComponentName name) {
}
    };service 如下:public class DownLoadService extends Service {
private static boolean downLoadResult =false;
private DownLoadBinder downLoadBinder  = new DownLoadBinder();
@Override
public IBinder onBind(Intent intent) {
return downLoadBinder;
}
public class  DownLoadBinder  extends Binder{
 
public DownLoadService getService(){
return DownLoadService.this;
}  }
/**imagePath:文件UIL fileName:文件进行重命名*/
boolean downlaodFile(final String imagePath, final String fileName) {
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File sdCardDir = Environment.getExternalStorageDirectory();// 获取到文件的存储路径
InputStream is = null;
try {
File dest = new File(sdCardDir.getCanonicalPath()+ File.separator + "imageMe");
is = ImageService.getInputStream(imagePath);
if (!dest.exists()) {
dest.mkdirs();
}
OutputStream output = new FileOutputStream(dest+ File.separator + fileName);
byte[] buffer = new byte[8192];
while ((is.read(buffer)) != -1) {
output.write(buffer);
}
output.flush();
System.out.println("下载成功");
downLoadResult = true;
} catch (Throwable e) {
e.printStackTrace();
downLoadResult = false;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
} } return downLoadResult;
}


}但总是报一个错nullPointException.  downLoadBinder 为空。如果把下载的代码放到绑定就会下载成功。为什么。要怎么改
。没有分了。就这么多吧。

解决方案 »

  1.   

    你绑定service的套路没有问题了 
    private  ServiceConnection connection =  new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, IBinder binder) {
                downLoadBinder = (DownLoadBinder)binder;
                
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };这里返回的downLoadBinder是空的吗?
      

  2.   

    你用显示调用试试,别用action调
      

  3.   

    我试了一下。效果一样。没有多大区别。那个service 一直是null .
      

  4.   

    private ServiceConnection connection = new ServiceConnection() {
      @Override
      public void onServiceConnected(ComponentName name, IBinder binder) {
      downLoadBinder = (DownLoadBinder)binder;
       
      }
      @Override
      public void onServiceDisconnected(ComponentName name) {
      }
      };你说你调试的时候不走这段代码?   那你把下载的方法放到 downLoadBinder = (DownLoadBinder)binder;下面是怎么好使的呢??
      

  5.   

    为什么不用AsyncTask试试呢 service干这个合适么?