创建新文件夹时出错

Error with creating new folder

你好,我正在尝试使用我的应用程序将我服务器上的一些文件下载到我的 phone。我正在尝试将其写入我的存储设备而不是我的 SD 卡!

我在清单中对我的设备拥有必要的权限:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />

这是我从 DownloaderClass 创建新文件夹的代码。我得到 return 下载完成...并且在调试器中它向我显示了将其保存在我想要的位置的路径,但是如果我看那里...文件夹应该位于的文件夹是空的。

        //File new_Folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "MY DOWNLOADED FILES");
        String intStorageDirectory = context.getFilesDir().toString();
        File new_Folder = new File(intStorageDirectory, "MY DOWNLOADED FILES");
        if (!new_Folder.exists()) {
            if (!new_Folder.mkdir()) {
                return "ERROR: mkdirs() failed for directory" + new_Folder.getAbsolutePath();
            }

        } else {
            Log.i("Info:", "Folder already exists");
        }

        /**
         * Create an output file to store the file for download
         */

        inputoutput_file = new File(new_Folder, "downloaded_files.jpg");


        InputStream inputStream = new BufferedInputStream(url.openStream(), 8192);
        //this will read the information in 1 KB
        byte[] data = new byte[1024];
        int total = 0;
        int count;

        OutputStream outputStream = new FileOutputStream(inputoutput_file);

        while ((count = inputStream.read(data))!= -1){
            total+=count;

            if (file_length>0) {
                int progress = total * 100 / file_length;
                publishProgress(progress);
                outputStream.write(data, 0, count);
                Log.i("Info:", "Progress: " + Integer.toString(progress));
            }
        }

        inputStream.close();
        outputStream.close();

        return "Download Complete...";

    } catch (MalformedURLException e) {
        e.printStackTrace();
        return "ERROR MalformedURLException" + e.getMessage();
    } catch (IOException e) {
        e.printStackTrace();
        return "ERROR IOException" + e.getMessage();
    }

}

在您的设备上转到您应用程序的设置,然后将存储权限开关切换为开。

已解决:只需将您的文件夹路径更改为

"/sdcard/insertyourpreferredfoldername/" 

并创建内部文件夹。调整

downloaded_file.jpg 

可变为您从服务器获得的文件名,它将单独保存文件!

编辑:感谢 greenapps 引导我走上正确的道路!