2015-04-17 110 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(); 
    } 

}