android存储各种格式文件到sd卡

 
  
 
  
 
  
public void saveFileToSDcard(String fileName,byte[]date){
    File file = null;
     //判断是否有sd卡
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
          //判断文件是什么格式存储到哪个文件夹
        if (fileName.endsWith(".mp3")){
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
        }else if (fileName.endsWith(".png")||fileName.endsWith(".jpg")){
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        }else if (fileName.endsWith(".mp4")){
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);

        }else{
            file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        }
        FileOutputStream outputStream = null;
        try {
            outputStream = new FileOutputStream(new File(file,fileName));
            outputStream.write(date,0,date.length);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if (outputStream!=null){
                try {
                     //关闭流
                    outputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

你可能感兴趣的:(android存储各种格式文件到sd卡)