0

Hi this is what i am trying to do to get the text files.I tried same with

  • New thread

also when i create new thread its not coming inside the getfiles(Drive services) method only. Please give me solution i am not getting this.

Error is runnung with main may lead to deadlock

public class MainActivity extends Activity {

  static final int REQUEST_ACCOUNT_PICKER = 1;


  private static Uri fileUri;
  private static Drive service;
  private GoogleAccountCredential credential;
  Button b;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    b = (Button)findViewById(R.id.b1);

    credential = GoogleAccountCredential.usingOAuth2(this, DriveScopes.DRIVE);
    startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);

    Toast.makeText(getApplicationContext(), "case1", Toast.LENGTH_LONG).show();

}

 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) 
 {

     if(requestCode==REQUEST_ACCOUNT_PICKER)
     {
         if (resultCode == RESULT_OK && data != null && data.getExtras() != null)
         {
             Toast.makeText(getApplicationContext(), "case2", Toast.LENGTH_LONG).show();

                String accountName = data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
                if (accountName != null)
                {
                  credential.setSelectedAccountName(accountName);
                  service = getDriveService(credential);

                  Toast.makeText(getApplicationContext(), "Got the E mail id", Toast.LENGTH_LONG).show();
             b.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "case4", Toast.LENGTH_LONG).show();

                    getfiles(service);

                }
              });
          }
        }
     }


 }

 public void getfiles(final Drive service)
 {



     Toast.makeText(getApplicationContext(), "case5", Toast.LENGTH_LONG).show();
     String TEXT_PLAIN = "TEXT/PLAIN";
     try
     {
     Files.List request = service.files().list().setQ("mimeType = '" + TEXT_PLAIN +"'");
     Toast.makeText(getApplicationContext(), "case", Toast.LENGTH_LONG).show();                 
     Map<String, File> textFiles = new HashMap<String, File>();

            do {

                  Toast.makeText(getApplicationContext(), "case6", Toast.LENGTH_LONG).show();

                FileList files = request.execute();  //this is error line


                Toast.makeText(getApplicationContext(), "case7", Toast.LENGTH_LONG).show();
                for (File file : files.getItems())
                {
                  textFiles.put(file.getId(), file);
                }
                request.setPageToken(files.getNextPageToken());

            } while (request.getPageToken() != null && request.getPageToken().length() > 0);

     }
     catch(Exception e)
     {
         Toast.makeText(getApplicationContext(),e.toString(), Toast.LENGTH_LONG).show();
     }



 }


 private Drive getDriveService(GoogleAccountCredential credential) {
        return new Drive.Builder(AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential)
            .build();
      }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

1 Answers1

0

You should do any code that could take a long time such as I/O or network activity on a different thread. In your situation your file retrieval would be best done within an ASyncTask.

The reason for this warning is because when your code is being executed to retrieve the file from Drive, the code will block until the file retrieval has completed, this means that nothing else within the program will be able to respond, i.e. GUI will not update and input from the user trying to do stuff with the GUI will not be registered. After a few seconds, if the file retrieval still isn't complete, then the user will be presented with an ANR (Application Not Responding) message. This will either give the user the choice of waiting, if their patient, or most likely force closing your app.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • I tried with with New thread also but when i tried to do that control is not coming to the function public void getfiles(final Drive service) Can you suggest me a modification for this. – Not using as Shivaraj Aug 23 '13 at 09:54
  • http://stackoverflow.com/questions/15057907/how-to-get-the-file-id-so-i-can-perform-a-download-of-a-file-from-google-drive-a. Maybe this will help. It shows how I am downloading a file when I was having an issue before – Boardy Aug 23 '13 at 10:23
  • Thanx... Its Happened Because i was using Toast here so it was searching for UI thread so... – Not using as Shivaraj Sep 01 '13 at 18:17