创建新文件夹时出错

时间:2017-10-24 21:22:51

标签: android createfile

您好我正在尝试使用我的应用将我服务器上的某些文件下载到手机上。我试图把它写在我的存储空间而不是我的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创建新文件夹的代码。我得到返回下载完成...并且在调试器中它向我显示它正确地将它保存在我想要的位置的路径,但是如果我看那里......文件夹应该在的文件夹是空的。< / p>

        //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();
    }

}

2 个答案:

答案 0 :(得分:0)

将设备转到应用的“设置”,然后将“存储空间的权限”切换为“打开”。

答案 1 :(得分:0)

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

即可
"/sdcard/insertyourpreferredfoldername/" 

并创建内部文件夹。

调整
downloaded_file.jpg 

变量到您从服务器获取的文件名,它将单独保存文件!

编辑:感谢greenapps  以正确的方式引导我!