【问题标题】:Android DownloadManager not know where the file downloadsAndroid DownloadManager 不知道文件下载到哪里
【发布时间】:2015-07-05 03:53:42
【问题描述】:

经过两天的测试,我们能够部分工作。
我下载了文件,但我不知道在哪里存储它。
我尝试下载图片,图片出现在文件夹/all_downloads

我不知道如何将它存储在 /sdcard/update 中。

import android.os.Environment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.app.Activity;
import android.app.DownloadManager;
import android.app.DownloadManager.Query;
import android.app.DownloadManager.Request;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import java.io.File;

public class Upgrade extends ActionBarActivity {
    private long enqueue;
    private DownloadManager dm;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_upgrade);

        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(
                            DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    Query query = new Query();
                    query.setFilterById(enqueue);
                    Cursor c = dm.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c
                                .getColumnIndex(DownloadManager.COLUMN_STATUS);
                        if (DownloadManager.STATUS_SUCCESSFUL == c
                                .getInt(columnIndex)) {


                        }
                    }
                }
            }
        };

        registerReceiver(receiver, new IntentFilter(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }

    public void onClick(View view) {
        dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
        Request request = new Request(
                Uri.parse("http://android.vrt.ro/tv-update/v1.apk"));
        enqueue = dm.enqueue(request);


    }

    public void showDownload(View view) {

        Intent promptInstall = new Intent(Intent.ACTION_VIEW)
                .setDataAndType(Uri.parse("file:///sdcard/download/v1.apk"),
                        "application/vnd.android.package-archive");
        startActivity(promptInstall);

    }

}

【问题讨论】:

    标签: java android eclipse import


    【解决方案1】:

    使用这个

    request.setDestinationInExternalPublicDir("/updates", "update.apk");
    

    在 API 级别 9 中添加

    public DownloadManager.Request setDestinationInExternalPublicDir (String dirType, String subPath)
    

    将下载文件的本地目标设置为 公共外部存储目录(由 getExternalStoragePublicDirectory(String)).

    MediaScanner 不会扫描下载的文件。但是可以制作 可通过调用allowScanningByMediaScanner() 进行扫描。

    参数 dirType 传递给的目录类型 getExternalStoragePublicDirectory(String)` subPath 中的路径 外部目录,包括目标文件名 返回此 object Throws IllegalStateException 如果外部存储目录 无法找到或创建。

    你也可以使用这个版本

    String updatePath = Environment.getExternalStorageDirectory() + File.separator + "Updates" + File.separator + "update.apk";
    request.setDestinationUri(Uri.fromFile(new File(updatePath)))
    

    【讨论】:

    • 你需要把这条线放在哪里?能给我看看么?是初学者并尝试让自己上线,但我在工作室 android 中出现红色感谢您的速度。谢谢
    • 把它放在这一行之前 "enqueue = dm.enqueue(request);"
    • 谢谢,就是你说的,去吧。我应该怎么做才能不下载2次?或者如果没有下载apk文件?如何调用函数来知道我应该看什么。我知识不多,但我想学习。再次感谢
    • 您应该检查是否 enqueue != 0...然后下载已经在进行中...确保完成后将其设置为 0,这样您就可以知道它不再下载.您可以通过执行类似 boolean isApkPresent = new File(filePath).exists(); 的操作来简单地检查它是否存在
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多