Java 在Android上不使用Dropbox API从Dropbox下载APK
我想在不使用API的情况下从dropbox下载APK。我有以下代码:Java 在Android上不使用Dropbox API从Dropbox下载APK,java,android,parsing,apk,dropbox,Java,Android,Parsing,Apk,Dropbox,我想在不使用API的情况下从dropbox下载APK。我有以下代码: @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); // Initialise the button btn = (Button) findViewById(R.id.
@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;
}
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
//初始化按钮
btn=(按钮)findViewById(R.id.mybutton);
btn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
新建下载文件任务().execute();
//显示下载确认
Toast confirmDownload=Toast.makeText(Main2Activity.this,“下载完成”,Toast.LENGTH\u LONG);
confirmDownload.show();
}
});
}
/**
*异步任务类
*将调用用于下载apk的新流
*/
私有静态类DownloadFilesTask扩展了AsyncTask{
/**
*从提供的路径下载文件的并发进程
*未使用方法的@param voids参数
*方法的@return值不重要
*/
@凌驾
受保护的空位背景(空位…空位){
试一试{
URL下载=新URL(路径至主键);
ReadableByteChannel rbc=Channels.newChannel(下载.openStream());
FileOutputStream fileOut=新的FileOutputStream(dowloadPath+“file.apk”);
fileOut.getChannel().transferFrom(rbc,0,1发送给任何收到此恼人错误的人
步骤1:将dropbox URL末尾的dl更改为1
步骤2:使用下载文件
工作起来很有魅力!不需要流等
private void download() {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(dropboxPath);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setDescription("new version");
request.setTitle("Updated App");
request.setMimeType("application/vnd.android.package-archive");
request.setDestinationInExternalPublicDir(pathApk,"new-app.apk");
// request.setDestinationUri(Uri.parse(pathApk + "test.txt"));
// request.setDestinationUri(Uri.parse(pathApk + "test.txt"));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Download will immediately
downloadManager.enqueue(request);
}
任何人都会犯这个讨厌的错误
步骤1:将dropbox URL末尾的dl更改为1
步骤2:使用下载文件
工作起来很有魅力!不需要流等
private void download() {
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse(dropboxPath);
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setDescription("new version");
request.setTitle("Updated App");
request.setMimeType("application/vnd.android.package-archive");
request.setDestinationInExternalPublicDir(pathApk,"new-app.apk");
// request.setDestinationUri(Uri.parse(pathApk + "test.txt"));
// request.setDestinationUri(Uri.parse(pathApk + "test.txt"));
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// Download will immediately
downloadManager.enqueue(request);
}
这是否回答了您的问题?感谢您的想法,将dl更改为1并不能解决问题。请参见下文。这是否回答了您的问题?感谢您的想法,将dl更改为1并不能解决问题。请参见下文