修改代码以将文件下载到音乐目录{Android}

时间:2014-01-11 15:26:41

标签: java android cordova

目前,以下代码仅将下载的文件保存到手机存储的下载目录中。因此,其他应用程序无法访问下载的文件,因为它是私有的。

我的问题是如何修改下面粘贴的代码以将下载的文件保存到Sd卡上的音乐目录中。  我粘贴了以下完整代码:

    private void downloadFile() {
    new DownloadFileThread().start();

}
class DownloadFileThread extends Thread {
    @Override
    public void run() {
        try {
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection) url
                        .openConnection();
                conn.connect();
                int length = conn.getContentLength();
                InputStream is = conn.getInputStream();

                FileOutputStream fos = getOutStream(fileName);
                int count = 0;
                byte buf[] = new byte[1024];
                do {
                    int numread = is.read(buf);
                    count += numread;
                    progress = (int) (((float) count / length) * 100);
                    mHandler.sendEmptyMessage(DOWNLOAD);
                    if (numread <= 0) {
                        mHandler.sendEmptyMessage(DOWNLOAD_FINISH);
                        break;
                    }
                    // 写入文件
                    fos.write(buf, 0, numread);
                } while (!cancelUpdate);
                fos.close();
                is.close();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
        mDownloadDialog.dismiss();
    }

}
/**
 * @param fileName
 * @return
 * @throws FileNotFoundException
 */
@SuppressWarnings("deprecation")
@SuppressLint("WorldReadableFiles")
private FileOutputStream getOutStream(String fileName) throws FileNotFoundException{
    if (Environment.getExternalStorageState().equals(
            Environment.MEDIA_MOUNTED)) {
        String sdpath = Environment.getExternalStorageDirectory()
                + "/";
        mSavePath = sdpath + "download";
        File file = new File(mSavePath);
        if (!file.exists()) {
            file.mkdir();
        }
        File saveFile = new File(mSavePath, fileName);
        return new FileOutputStream(saveFile);
    }else{
        mSavePath = mContext.getFilesDir().getPath();
        return mContext.openFileOutput(fileName , Context.MODE_WORLD_READABLE);
    }
}

}

0 个答案:

没有答案