I want to download an APK from dropbox without using the API. I have the following code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// Initialise the button
btn = (Button) findViewById(R.id.mybutton);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new DownloadFilesTask().execute();
// Show Download confirmation
Toast confirmDownload = Toast.makeText(Main2Activity.this,"Download is complete",Toast.LENGTH_LONG);
confirmDownload.show();
}
});
}
/**
* Asynchronous Task Class
* Will invoke a new stream with which the apk is to be downloaded
*/
private static class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
/**
* Concurrent process to download the file from the path provided
* @param voids Parameters of the method are not used
* @return Return value of the method is unimportant
*/
@Override
protected Void doInBackground(Void... voids) {
try{
URL download = new URL(pathToAPK);
ReadableByteChannel rbc= Channels.newChannel(download.openStream());
FileOutputStream fileOut = new FileOutputStream(dowloadPath+ "file.apk");
fileOut.getChannel().transferFrom(rbc, 0,1 << 24 );
fileOut.flush();
fileOut.close();
rbc.close();
}catch(Exception e){ e.printStackTrace(); }
return null;
}
The file downloads successfully however when I try to open the file I get a:
Parse error: There was a problem while parsing the package
My guess is the file is corrupted for some reason. Any guesses?