本文主要是介绍32 Android 保存到Sdcard public文件夹下,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
/*** 保存到Sdcard public文件夹下* @param fileName* @param data*/public void saveToFileSdcardBySuff(String fileName,byte[] data){//保存文件的目录File file=null;FileOutputStream outputStream=null;if(Environment.getExternalStorageDirectory().equals(Environment.MEDIA_MOUNTED)){if(fileName.endsWith(".mp3")){file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);}else if(fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")){file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);}else if(fileName.endsWith(".mp4")||fileName.endsWith(".3gp")||fileName.endsWith(".txt")){file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);}else{file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);}}try {outputStream=new FileOutputStream(new File(file, fileName));outputStream.write(data, 0, data.length);} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}finally{if(outputStream != null){try {outputStream.close();} catch (Exception e2) {// TODO: handle exceptione2.printStackTrace();}}}}
测试类代码:
public void save2() {FileService service = new FileService();service.saveToFileSdcardBySuff("abc.txt", "myTxt".getBytes());}
这篇关于32 Android 保存到Sdcard public文件夹下的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!