下载并播放mp3文件到内部存储

时间:2016-09-28 06:17:39

标签: android file storage internal downloading

我们正在开发android应用程序,其中mp3文件应该下载到后台服务的内部存储中。我们使用下面提到的intent service内部的代码片段实现了这一点,但是当我们尝试播放mp3时,我们收到错误,就像播放器不支持此音频文件一样。如果任何人有解决此问题的方法,请告知我同样的问题。

代码段:

    String urlPath = intent.getStringExtra(URL);
    String fileName = intent.getStringExtra(FILENAME);
    //  String fileName = Environment.getExternalStorageDirectory().toString() + "/trail.mp3";
    File output = new File(Environment.getExternalStorageDirectory(),
            fileName);

    File dir = new File(output.getAbsolutePath()
            + "/TRIALS/");

    dir.mkdirs();
    String path = dir + "/" +"trail" + ".mp3";
    File f = new File(path);


    if (f.exists()) {
        f.delete();
    }

    InputStream stream = null;
    FileOutputStream fos = null;
    try {

        URL url = new URL(urlPath);
        URLConnection conection = url.openConnection();
        conection.connect();
        // getting file length
        int lenghtOfFile = conection.getContentLength();
        //URL url = new URL(urlPath);
        InputStream input = new BufferedInputStream(url.openStream());

        stream = url.openConnection().getInputStream();
        InputStreamReader reader = new InputStreamReader(stream);
        fos = new FileOutputStream(f.getPath());

        byte data[] = new byte[lenghtOfFile];

        long total = 0;

        int times = -1;
        while ((times = reader.read()) != -1) {
            fos.write(times);

        }
        // Successful finished
        result = Activity.RESULT_OK;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fos != null) {
            try {
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    publishResults(output.getAbsolutePath(), result);
}

1 个答案:

答案 0 :(得分:0)

1.检查urlPath是否正确。您可以通过资源管理器打开它,并检查是否开始下载.mp3

2.将资源管理器获取的文件大小与通过代码下载的文件进行比较。

3.如果尺寸不同,则表示下载失败。请尝试修改您的代码:

    String urlPath = intent.getStringExtra(URL);
    String fileName = intent.getStringExtra(FILENAME);
    File output = new File(Environment.getExternalStorageDirectory(),
            fileName);
    File dir = new File(output.getAbsolutePath()
            + "/TRIALS/");
    dir.mkdirs();
    String path = dir + "/" +"trail" + ".mp3";
    File f = new File(path);

    if (f.exists()) {
        f.delete();
    }

    InputStream stream = null;
    FileOutputStream fos = null;
    try {

        URL url = new URL(urlPath);
        URLConnection urlcon = url.openConnection();
        stream = urlcon.getInputStream();

        InputStreamReader reader = new InputStreamReader(stream);
        fos = new FileOutputStream(f.getPath());

        int times = -1;
        while ((times = reader.read()) != -1) {
            fos.write(times);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        if (fos != null) {
            try {
                fos.flush();
                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

我删除了一些无用的代码,并在fos.flush()之前添加fos.close()