2015-04-17 108 views
0

我使用下面的代碼在圖片文件夾中創建文件夾並烘烤路徑。 但我沒有提到在android清單中寫入外部存儲的文件夾。創建文件夾時出錯

java.io.File file = new java.io.File (Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"College"); 
    if(!file.exists()) 
    { 
     if(file.mkdir()) 
     { 

      String f =file.getAbsolutePath(); 
      Toast.makeText(getApplicationContext(),f,Toast.LENGTH_LONG).show(); 
     } 
     else { 
      Toast.makeText(getApplicationContext(),"File Not Created",Toast.LENGTH_LONG).show(); 

     } 

    } 

請幫助我的問題

+1

請您menifest檢查許可<使用權限android:name =「android.permission.WRITE_EXTERNAL_STORAGE」/> – rahultheOne

+0

我已經添加了權限 – Pragadees

+0

使用'file.mkdirs()'而不是'file.mkdir()' –

回答

0

這爲我做:

import java.io.File; 

File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES + "/College"); 

    try 
    { 
     if (!file.exists()) { 
      file.mkdir(); 
      Toast.makeText(this, "Folder created at " + file.toString(), Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception ex) { 
     ex.printStackTrace(); 
    } 
0

我用這個代碼和它的工作

File file; 
    String CAMERA_DIR = "/dcim/"; 

    // Check that the SDCard is mounted 
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { 

     // Get the absolute path 
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) { 
      file = new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "College"); 
     } 
     else { 
      file = new File(Environment.getExternalStorageDirectory() + CAMERA_DIR + "College"); 
     } 


     // Create the storage directory if it does not exist 
     if (! file.exists()) { 
      if (! file.mkdirs()) { 
       Toast.makeText(getApplicationContext(),"File Not Created",Toast.LENGTH_LONG).show(); 
       return; 
      } 
     } 

     String f =file.getAbsolutePath(); 
     Toast.makeText(getApplicationContext(),f,Toast.LENGTH_LONG).show(); 
    } 
    else { 
     throw new IOException(); 
    } 

}