Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/docker/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试在android中创建文件夹并保存mp3文件失败_Android_Android Asynctask_Storage - Fatal编程技术网

尝试在android中创建文件夹并保存mp3文件失败

尝试在android中创建文件夹并保存mp3文件失败,android,android-asynctask,storage,Android,Android Asynctask,Storage,我有一个在线音乐播放器,里面有一个按钮用来下载文件。有一个“progressDialog”可以很好地工作,它显示了下载文件的进度,看起来它真的在下载我的文件。但完成后,我的设备上没有文件夹或文件 我还在清单中添加了写外部存储权限 这是我的下载课程: public class DownloadTask extends AsyncTask<String, Integer, String> { @SuppressLint("StaticFieldLeak") priva

我有一个在线音乐播放器,里面有一个按钮用来下载文件。有一个“progressDialog”可以很好地工作,它显示了下载文件的进度,看起来它真的在下载我的文件。但完成后,我的设备上没有文件夹或文件

我还在清单中添加了写外部存储权限

这是我的下载课程:

public class DownloadTask extends AsyncTask<String, Integer, String> {
@SuppressLint("StaticFieldLeak")
private Context context;
public static ProgressDialog progressBar;

public DownloadTask(Context context) {
    this.context = context;
    progressBar = new ProgressDialog(context);
    progressBar.setMessage("Downloading...");
    progressBar.setIndeterminate(true);
    progressBar.setCancelable(true);
    progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    progressBar.show();
}

@Override
protected String doInBackground(String... strings) {
    InputStream inputStream = null;
    OutputStream outputStream = null;
    HttpURLConnection connection = null;

    try {
        URL url = new URL(strings[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();

        int fileLength = connection.getContentLength();
        inputStream = connection.getInputStream();
        fileCache();
        outputStream = new FileOutputStream(context.getFilesDir() + "listening"
                + strings[1] + ".mp3");
        byte[] data = new byte[4096];
        long total = 0;
        int count;
        while ((count = inputStream.read(data)) != -1) {
            total += count;
            if (fileLength > 0)
                publishProgress((int) (total * 100 / fileLength));
            outputStream.write(data, 0, count);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (outputStream != null) {
                outputStream.flush();
                outputStream.close();
            }
            if (inputStream != null)
                inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (connection != null)
            connection.disconnect();
    }
    return null;
}

@Override
protected void onProgressUpdate(Integer... values) {
    super.onProgressUpdate(values);
    progressBar.setIndeterminate(false);
    progressBar.setMax(100);
    progressBar.setProgress(values[0]);
}

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);
    progressBar.dismiss();
    if (s != null) {
        Toast.makeText(context, "Error while Downloading", Toast.LENGTH_SHORT).show();
    } else {
        Toast.makeText(context, "Downloaded successfully", Toast.LENGTH_SHORT).show();
    }
}

private void fileCache() {
        File myDir = new File(context.getFilesDir(), "listening");
        if (!myDir.exists()) {
            myDir.mkdirs();
        }
}
DownloadTask downloadTask = new DownloadTask(context);
downloadTask.execute(extra.getString("link"), extra.getString("title"));

“但完成后,我的设备上没有文件夹或文件”-您如何查找它?您正在编写Android SDK所指的内容,因此只能通过Android Studio的设备文件资源管理器或
adb
查看结果。“我还在清单中添加了“写入外部存储”权限”--这无关紧要,因为您没有使用。@我在物理和模拟器的文件管理器上都检查了Commonware。你有什么建议?我的意思是如何更改我的代码以真正保存文件?“你有什么建议?”--如果你希望你的文件显示在那里,你需要使用Android SDK所指的。“我的意思是如何更改代码以真正保存文件?”--它已经在保存文件了(可能--您的代码已经过时多年了)。它只是没有将它们保存在您期望的地方。@commonware所以我担心如果一些用户没有外部存储,这就是为什么我要使用内部存储。我还研究了你的文件。你能帮我写代码吗?“但完成后,我的设备上没有文件夹或文件”——你是如何查找的?您正在编写Android SDK所指的内容,因此只能通过Android Studio的设备文件资源管理器或
adb
查看结果。“我还在清单中添加了“写入外部存储”权限”--这无关紧要,因为您没有使用。@我在物理和模拟器的文件管理器上都检查了Commonware。你有什么建议?我的意思是如何更改我的代码以真正保存文件?“你有什么建议?”--如果你希望你的文件显示在那里,你需要使用Android SDK所指的。“我的意思是如何更改代码以真正保存文件?”--它已经在保存文件了(可能--您的代码已经过时多年了)。它只是没有将它们保存在您期望的地方。@commonware所以我担心如果一些用户没有外部存储,这就是为什么我要使用内部存储。我还研究了你的文件。你能帮我查一下密码吗?