在Android中构建新目录并推送文件

时间:2012-02-13 04:08:47

标签: android

我知道在我的另一个项目中工作的代码如果它尚不存在则创建一个新文件夹,并将我的项目中的文件放入其中。但在我的新项目中,代码什么也没做。我做错了什么?

File directory = new File(Environment.getExternalStorageDirectory()
                + File.separator + "testing");
            directory.mkdirs();

        File directory2 = new File(Environment.getExternalStorageDirectory()
                + File.separator + "CandooData" + File.separator + "Meds");
        if (!(directory2.isDirectory())) {
            directory2.mkdirs();
        }
        File directory3 = new File(Environment.getExternalStorageDirectory()
                + File.separator + "CandooData" + File.separator + "Meds");
        String[] files;
        if (directory3.isDirectory()) {
            files = directory3.list();
            if (files.length == 0) {
                // directory is empty
                CopyAssets("medsxml", "/Meds");
            }
        }

4 个答案:

答案 0 :(得分:2)

  1. 通过logcat查看日志。

  2. 您可能错过了WRITE_EXTERNAL_STORAGE权限。

  3. mkdirs()可以正常工作,而不是比mkdir()差。

答案 1 :(得分:1)

使用此 directory.mkdir(); 代替此 directory.mkdirs();

答案 2 :(得分:1)

以下是简要示例

 File folder = new File(Environment.getExternalStorageDirectory()+"/Trip");
        if(!folder.exists()){
            folder.mkdir();
        }
           File newxmlfile = new File(Environment.getExternalStorageDirectory()+"/Trip/"+ RecordID +".xml");
           try{
               newxmlfile.createNewFile();
           }catch(IOException e){

           }

答案 3 :(得分:0)

我正在使用以下方法创建目录并将文件保存到其中。

private static File APP_FILE_PATH = new File("/sdcard/SpeechTutor");

 p_file_Side_Slow = new File("/sdcard/SpeechTutor/P_SideMovieSlow.mp4");
        p_file_Side_Medium = new File("/sdcard/SpeechTutor/P_SideMovieMedium.mp4");
        p_file_Side_Fast = new File("/sdcard/SpeechTutor/P_SideMovieFast.mp4");

        p_file_Front_Slow = new File("/sdcard/SpeechTutor/P_FrontMovieSlow.mp4");
        p_file_Front_Medium = new File("/sdcard/SpeechTutor/P_FrontMovieMedium.mp4");
        p_file_Front_Fast = new File("/sdcard/SpeechTutor/P_FrontMovieFast.mp4");

 if((!(p_file_Side_Slow.exists())) || (!(p_file_Side_Medium.exists())) || (!(p_file_Side_Fast.exists())) 
                || (!(p_file_Front_Slow.exists()))|| (!(p_file_Front_Medium.exists()))||(!(p_file_Front_Fast.exists()))){
            ArrayList<String> files = new ArrayList<String>();
            files.add("P_SideMovieSlow.mp4");
            files.add("P_SideMovieMedium.mp4");
            files.add("P_SideMovieFast.mp4");

            files.add("P_FrontMovieSlow.mp4");
            files.add("P_FrontMovieMedium.mp4");
            files.add("P_FrontMovieFast.mp4");
            new myAsyncTask().execute(files);
         }



// AsyncTass for the Progress Dialog and to do Background Process
    private class myAsyncTask extends AsyncTask<ArrayList<String>, Void, Void> {         
        ArrayList<String> files;         
        ProgressDialog dialog;         
        @Override         
        protected void onPreExecute() {             
            dialog = ProgressDialog.show(MainScreenActivity.this, "Speech Tutor", "Loading...");         
        }         
        @Override         
        protected Void doInBackground(ArrayList<String>... params) {              
            files = params[0];             
            for (int i = 0; i < files.size(); i++) {                 
                copyFileFromAssetsToSDCard(files.get(i));                
            }             return null;         
        }         
        @Override         
        protected void onPostExecute(Void result) {             
            dialog.dismiss();   
//          myVideoThumbnail.setImageBitmap((Bitmap)ThumbnailUtils.createVideoThumbnail(p_file_Side_Slow.getAbsolutePath(),MediaStore.Video.Thumbnails.MINI_KIND));
//          myVideoView.setVideoURI(p_uri_Side_Medium);
            setVideoURI(sideShow, frontshow, slowShow, mediumShow, fastShow);
        }      
    } 


// Function to copy file from Assets to the SDCard
    public void copyFileFromAssetsToSDCard(String fileFromAssets){
        AssetManager is = this.getAssets();
        InputStream fis;
        try {
            fis = is.open(fileFromAssets);
            FileOutputStream fos;
            if (!APP_FILE_PATH.exists()) {
                APP_FILE_PATH.mkdirs();
            }
            fos = new FileOutputStream(new File(Environment.getExternalStorageDirectory()+"/SpeechTutor",fileFromAssets));
            byte[] b = new byte[8];
            int i;
            while ((i = fis.read(b)) != -1) {
                fos.write(b, 0, i);
            }
            fos.flush();
            fos.close();
            fis.close();
        } 
        catch (IOException e1) {
            e1.printStackTrace();
        }
    }

以上所有代码都会将文件保存为后台进程。