【问题标题】:Download APK from Dropbox without the Dropbox API on Android在 Android 上不使用 Dropbox API 从 Dropbox 下载 APK
【发布时间】:2020-06-16 08:22:26
【问题描述】:

我想在不使用 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.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;
    }

文件下载成功,但是当我尝试打开文件时,我得到一个:

解析错误:解析包时出现问题

我的猜测是文件由于某种原因已损坏。有什么猜测吗?

【问题讨论】:

标签: java android parsing apk dropbox


【解决方案1】:

致任何遇到此烦人错误的人。

第 1 步:将 Dropbox URL 末尾的 dl 更改为 1

第 2 步:使用DownloadManager 下载文件

像魅力一样工作!不需要流等。

    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);

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多