正在下载的文件保存在数据文件夹中

时间:2013-07-13 05:21:47

标签: android path download download-manager

我正在使用下载管理器从我的服务器下载MP3文件。

以下是代码。

public String createFilePath()
    {
        String path;
        String dir  = "APP_NAME";

        path = Environment.getExternalStorageDirectory().getPath();



        File file = new File(Environment.getExternalStorageDirectory(),dir);
        if(!file.exists())
        {
            file.mkdir();
        }

        path +=   "/" +dir + "/";

        System.out.println("-- saving path : " + path);
        return path;
    }

    public void startDownload() {
        Uri uri=Uri.parse(URLFixer.Fix(DATA.url));




        System.out.println("-- download path : " + createFilePath() + FileNameGetter.getFileName(DATA..url));

        DownloadManager.Request request = new Request(uri);
        request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI |DownloadManager.Request.NETWORK_MOBILE);
        request.setAllowedOverRoaming(false);
        request.setTitle(DATA..title);
        request.setDescription(DATA.artist + " - " + DATA.album);


        request.setDestinationInExternalFilesDir(activity, createFilePath(), FileNameGetter.getFileName(DATA..url));
        //      request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);




        if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
            //                  request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN);
            request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        }


        lastDownload=   mgr.enqueue(request);
        isDownloading = true;
        Toasts.pop(activity, "Download Started!!");

        //      v.setEnabled(false);
        //      findViewById(R.id.query).setEnabled(true);
    }

问题是它应该保存在文件夹“APP_NAME”内的SD卡上,但是一旦下载音频,我就无法在该文件夹中看到它,当我播放音频并检查其信息时,它会显示路径喜欢这个

/sdcard/Android/data/com.X.app/files/mnt/sdcard/APP_NAME/file.mp3

由于它被保存在数据文件夹中,因此用户无法看到该文件。如何修复它以将其移动到主SD卡,即/ mnt / sdcard / APP_NAME,以便用户可以看到它。

1 个答案:

答案 0 :(得分:0)

DownloadManager.Request.setDestinationUri(Uri uri)应符合您的要求,如果您希望MediaScanner扫描MP3,请记得致电allowScanningByMediaScanner()