//访问sdcar需要授权WRITE_EXTERNAL_STORAGE
1 //---------------------写----------------------- 2 public boolean saveFileToSdcard(String fileName, byte[] data) { 3 boolean flag = false; 4 // 先判断sdcard的状态; 5 String state = Environment.getExternalStorageState(); 6 // 表示sdcard挂载在手机上,并且可以读写 7 FileOutputStream outputStream = null; 8 // 获得sdcard的根目录/mnt/sdcard/..... 9 File root = Environment.getExternalStorageDirectory(); 10 11 if (state.equals(Environment.MEDIA_MOUNTED)) { 12 // File file =new File(root, fileName);//在sdcard根目录上创建一个文件 13 File file = new File(root.getAbsolutePath() + "/txt");// 创建一个TXT文件夹再存 14 if (!file.exists()) { 15 file.mkdirs(); 16 } 17 try { 18 // outputStream=new FileOutputStream(file); 19 outputStream = new FileOutputStream(new File(file, fileName)); 20 outputStream.write(data, 0, data.length); 21 flag = true; 22 } catch (Exception e) { 23 // TODO: handle exception 24 e.printStackTrace(); 25 } finally { 26 if (outputStream != null) { 27 try { 28 outputStream.close(); 29 } catch (Exception e2) { 30 // TODO: handle exception 31 e2.printStackTrace(); 32 } 33 34 } 35 36 } 37 38 } 39 return flag; 40 }
-------------测试------------------------
1 public void save(){ 2 FileService service=new FileService(); 3 service.saveFileToSdcard("aa.txt", "zy520".getBytes()); 4 5 }
----------------------------------
1 //----------------------------------读文件内容----------------------- 2 public String readContextFromSdcard(String fileName) { 3 File root=Environment.getExternalStorageDirectory(); 4 FileInputStream inputStream=null; 5 ByteArrayOutputStream outputStream=new ByteArrayOutputStream(); 6 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 7 File file = new File(root.getAbsolutePath() + "/txt/"); 8 File file2=new File(file, fileName); 9 int len=0; 10 byte[]data=new byte[1024]; 11 12 13 if (file2.exists()) { 14 try { 15 inputStream=new FileInputStream(file2); 16 while((len=inputStream.read(data))!=-1){ 17 outputStream.write(data, 0, len); 18 19 20 } 21 return new String(outputStream.toByteArray()); 22 } catch (Exception e) { 23 // TODO: handle exception 24 e.printStackTrace(); 25 }finally{ 26 if (inputStream!=null) { 27 try { 28 inputStream.close(); 29 } catch (Exception e2) { 30 // TODO: handle exception 31 e2.printStackTrace(); 32 } 33 34 } 35 } 36 37 } 38 } 39 40 return null; 41 }
------------测试--------------------
1 public void read(){ 2 FileService service=new FileService(); 3 String msg =service.readContextFromSdcard("aa.txt"); 4 System.out.println("---->>"+msg); 5 6 7 }
-------------下载图片到sdcard再从sdcard中读出来----------------------------
1 public class ImageCache { 2 public static String saveImageCache(String fileName, byte[] data) { 3 4 File file = Environment.getExternalStorageDirectory(); 5 FileOutputStream outputStream = null; 6 if (Environment.getExternalStorageState().equals( 7 Environment.MEDIA_MOUNTED)) { 8 try { 9 outputStream = new FileOutputStream(new File(file, fileName)); 10 outputStream.write(data, 0, data.length); 11 12 return file.getAbsolutePath()+"/"+fileName; 13 } catch (Exception e) { 14 // TODO: handle exception 15 e.printStackTrace(); 16 }finally{ 17 if (outputStream!=null) { 18 try { 19 outputStream.close(); 20 } catch (Exception e2) { 21 // TODO: handle exception 22 e2.printStackTrace(); 23 } 24 } 25 } 26 } 27 28 return null; 29 } 30 }
---------------------------------------------
1 public class MainActivity extends ActionBarActivity { 2 private Button button; 3 private ImageView imageView; 4 private ProgressDialog dialog; 5 private String path ="https://www.baidu.com/img/bd_logo1.png"; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.fragment_main); 11 dialog = new ProgressDialog(this); 12 dialog.setTitle("提示"); 13 dialog.setMessage("load....."); 14 button = (Button) findViewById(R.id.button1); 15 imageView = (ImageView) findViewById(R.id.imageView1); 16 button.setOnClickListener(new View.OnClickListener() { 17 18 @Override 19 public void onClick(View v) { 20 // TODO Auto-generated method stub 21 new MyTask().execute(path); 22 } 23 }); 24 25 } 26 27 class MyTask extends AsyncTask<String, Void, byte[]> { 28 private String imageName; 29 @Override 30 protected void onPreExecute() { 31 // TODO Auto-generated method stub 32 super.onPreExecute(); 33 dialog.show(); 34 } 35 36 @Override 37 protected byte[] doInBackground(String... params) { 38 // TODO Auto-generated method stub 39 try { 40 String name=params[0]; 41 //截取图片名称:bd_logo1.png 42 imageName=name.substring(name.lastIndexOf("/")+1, name.length()); 43 } catch (Exception e) { 44 // TODO: handle exception 45 } 46 47 return HttpUtils.getImage(params[0]); 48 } 49 50 @Override 51 protected void onPostExecute(byte[] result) { 52 // TODO Auto-generated method stub 53 super.onPostExecute(result); 54 if (result != null) { 55 Bitmap bitmap=BitmapFactory.decodeByteArray(result, 0, result.length); 56 imageView.setImageBitmap(bitmap); 57 ImageCache.saveImageCache(imageName, result); 58 59 } else { 60 imageView.setImageResource(R.drawable.ic_launcher); 61 } 62 63 dialog.dismiss(); 64 } 65 66 }
1 public class HttpUtils { 2 public static byte[] getImage(String path){ 3 byte[]data=null; 4 HttpClient httpClient=new DefaultHttpClient(); 5 HttpPost post =new HttpPost(path); 6 try { 7 HttpResponse response=httpClient.execute(post); 8 9 if (response.getStatusLine().getStatusCode()==200) { 10 data=EntityUtils.toByteArray(response.getEntity()); 11 } 12 } catch (Exception e) { 13 // TODO: handle exception 14 e.printStackTrace(); 15 }finally{ 16 httpClient.getConnectionManager().shutdown(); 17 } 18 19 20 return data; 21 } 22 }
、、------------------------------------------------------
1 //---------------保存指定的SDcard目录---------- 2 public void saveFileToSdcardBySuff(String fileName,byte [] data){ 3 4 //保存文件的目录 5 File file=null; 6 if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { 7 if (fileName.endsWith(".mp3")) { 8 file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC); 9 10 }else if (fileName.endsWith(".jpg")||fileName.endsWith(".png")||fileName.endsWith(".gif")) { 11 file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 12 }else if (fileName.endsWith(".mp4")||fileName.endsWith(".3gp")) { 13 file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); 14 15 }else { 16 file=Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); 17 } 18 } 19 FileOutputStream outputStream=null; 20 try { 21 outputStream=new FileOutputStream(new File(file,fileName)); 22 outputStream.write(data,0, data.length); 23 } catch (Exception e) { 24 // TODO: handle exception 25 e.printStackTrace(); 26 }finally{ 27 try {if (outputStream!=null) { 28 outputStream.close(); 29 } 30 31 } catch (Exception e2) { 32 // TODO: handle exception 33 } 34 35 } 36 37 }
------------------------------测试---------------------------------------
1 public void save2(){ 2 FileService service=new FileService(); 3 service.saveFileToSdcardBySuff("aa.txt", "zy520".getBytes()); 4 5 } 6
---------------------------删除----------------------------------------
public boolean deleteFileFromSdcard(String folder,String fileName){ boolean flag=false; File file=Environment.getExternalStorageDirectory(); if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { File exitfile=new File(file.getAbsolutePath()+"/"+folder+"/"+fileName); if (exitfile.exists()) { flag=exitfile.delete(); } } return flag; }
----------------测试-------------------------
1 public void del(){ 2 FileService service=new FileService(); 3 boolean flag =service.deleteFileFromSdcard("txt","aa.txt"); 4 System.out.println("---->>"+flag); 5 6 7 }
时间: 2024-10-23 23:08:26