开发者

AsyncTask Force Close

开发者 https://www.devze.com 2023-04-11 06:01 出处:网络
Anyone know why AsyncTask is force closing when I try to unzip a file in the background, it works when not using AsyncTask so I\'m not sure what I am doing wrong:

Anyone know why AsyncTask is force closing when I try to unzip a file in the background, it works when not using AsyncTask so I'm not sure what I am doing wrong:

public class UnzipFile extends AsyncTask<Void, Integer, String> {                                                        
String zipFile = Environment.getExternalStorageDirectory() + "/download/" + mixtapefilename; 
String unzipLocation = Environment.getExternalStorageDirectory() + "/download/";

    @Override
    protected void onPreExecute() {         
        Toast toast = Toast.makeText(getApplicationContext(), "Unzipping...", Toast.LENGTH_SHORT);
        toast.show();

       /* Intent intent = new Intent();
        final PendingIntent pendingIntent = PendingIntent.getActivity(Download.this, 0, intent, 0);

        // configure the notification
        notification = new Notification(R.drawable.zipicon, "Unzipping...", System
                .currentTimeMillis());
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        notification.setLatestEventInfo(getApplicationContext(), "Please Wait", "Unzipping...", pendingIntent);
        notificationManager2.notify(1, notification);*/

    }

    @Override
    protected String doInBackground(Void... params) {                         
        Decompress d = new Decompress(zipFile, unzipLocation); 
        d.unzip();               

        return "success";}                              

    @Override
    protected void onPostExecute(String result) {
        Toast toast = Toast.makeText(getApplicationContext(), "Download Complete", Toast.LENGTH_SHORT);
        toast.show();
        File oldzip = new File(zipFile);
        boolean deleted = oldzip.delete();
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
        /*notificationManager2.cancel(1);*/                             
    }
}       

and then here is my unzip portion that I am trying to run in the background

public class De开发者_运维问答compress { 
      byte[] buffer = new byte[1024];
      int length;
      private String _zipFile; 
      private String _location; 

      public Decompress(String zipFile, String location) { 
        _zipFile = zipFile; 
        _location = location; 
       File a = new File(_location + foldername); 
        if(!a.isDirectory()){
            a.mkdirs();
        }                        

        _dirChecker(""); 
      } 

      public void unzip() { 

        try  { 
          FileInputStream fin = new FileInputStream(_zipFile); 
          ZipInputStream zin = new ZipInputStream(fin); 
          ZipEntry ze = null; 
          while ((ze = zin.getNextEntry()) != null) { 


            if(ze.isDirectory()) { 
              _dirChecker(ze.getName()); 
            } else { 
              FileOutputStream fout = new FileOutputStream(_location + ze.getName()); 
              while ((length = zin.read(buffer))>0) {
                  fout.write(buffer, 0, length);
                  }

              zin.closeEntry(); 
              fout.close(); 
            } 

          } 
          zin.close(); 
          Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG);
          toast.show();
        } catch(Exception e) { 
            ProgressDialog dialog;
         dialog = new ProgressDialog(Download.this);                
         dialog.setMessage(e.toString());
         dialog.show(); 
        } 

      } 

      private void _dirChecker(String dir) { 
        File f = new File(_location + dir); 


        if(!f.isDirectory()) { 
          f.mkdirs(); 
        } 
      } 
    } 


Since you are using AsynTask you can not communicate with main UI thread (i.e. display toast messages) inside doInBackground().

The only way to communicate with the main UI thread during AsynTask is by using onPreExcute() or onPostExcute()

So just remove this line:

Toast toast = Toast.makeText(getApplicationContext(),"Download Complete", Toast.LENGTH_LONG);


I'm sorry to disagree but there is several ways to execute code in the UI thread from another thread : Activity.runOnUiThread(Runnable) View.post(Runnable) View.postDelayed(Runnable, long) you could use one of those to post something that will be executed by the UI Thread regardless of the thread you are currently in. But yes you could just get them away and use the Log.d(String, String) instead (for instance)


UI Thread is non Thread-safe. So it's so dangerous to access UI Thread in other thread. You must post your message to UI message queue by View.post() method

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号