2015-07-22 324 views
2

我有一個使用通道將文本文件下載到本地文件的asynctask。AsyncTask致命異常#1

class getUrlsClass extends AsyncTask<String, Integer, File>{ 

    @Override 
    protected File doInBackground(String... params) { 
     URLConnection uc; 
     File urlFiles= new File(getApplicationContext().getFilesDir(), "urls"); 
     try { 
      URL url = new URL(params[0]); 
      uc = url.openConnection(); 
      uc.setUseCaches(false); 
      ReadableByteChannel rbc = Channels.newChannel(uc.getInputStream()); 
      FileOutputStream fos = new FileOutputStream(urlFiles); 
      fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 
      fos.close(); 


     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     return null; 
    } 

} 

,我得到一個致命的運行時異常,會使得程序崩潰的開始:

07-22 01:31:01.769: E/AndroidRuntime(27610): FATAL EXCEPTION: AsyncTask #1 
07-22 01:31:01.769: E/AndroidRuntime(27610): Process: com.example.griding, PID: 27610 
07-22 01:31:01.769: E/AndroidRuntime(27610): java.lang.RuntimeException: An error occured while executing doInBackground() 
07-22 01:31:01.769: E/AndroidRuntime(27610): at android.os.AsyncTask$3.done(AsyncTask.java:300) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.setException(FutureTask.java:222) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.run(FutureTask.java:242) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.lang.Thread.run(Thread.java:841) 
07-22 01:31:01.769: E/AndroidRuntime(27610): Caused by: java.lang.IllegalArgumentException: position=0 count=9223372036854775807 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.nio.FileChannelImpl.transferFrom(FileChannelImpl.java:368) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at com.example.myApp.DownloadManagerActivity$getUrlsClass.doInBackground(DownloadManagerActivity.java:284) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at com.example.myApp.DownloadManagerActivity$getUrlsClass.doInBackground(DownloadManagerActivity.java:1) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at android.os.AsyncTask$2.call(AsyncTask.java:288) 
07-22 01:31:01.769: E/AndroidRuntime(27610): at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
07-22 01:31:01.769: E/AndroidRuntime(27610): ... 4 more 

什麼是任務的問題?

+0

我假設你在清單中使用'INTERNET'權限。你可以請調試'fos.getChannel()。transferFrom(rbc,0,Long.MAX_VALUE);'行並告訴'rbc'的值 –

+0

它是'java.nio.channels.Channels $ InputStreamChannel @ 95e1fd08' @Anand –

回答

2

唯一的例外是在引起:

fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); 

因爲參數count不能超過Integer.MAX_VALUE更大。這是造成這種特殊的例外!

FileChannelImpl類文件發現這一點:

if (position < 0 || count < 0 || count > Integer.MAX_VALUE) { 
     throw new IllegalArgumentException("position=" + position + " count=" + count); 
    } 

希望它能幫助! :)

相關問題