Error while Retriving files from google drive using drive API. "Calling
this from your main thread can lead to deadlock
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;
}
}
No comments:
Post a Comment