Android 将文件保存到SD卡,从卡中取文件,及删除文件

时间:2022-10-30 15:09:33

//保存到SD卡

private static String sdState = Environment.getExternalStorageState();
    private static String path = Environment.getExternalStorageDirectory().toString();
    public static void saveBitmap(Bitmap bitmap,String imageName)
    { 
     File file;
     File PicName;
     if(sdState.equals(Environment.MEDIA_MOUNTED))
     {
      //获得sd卡根目录
      file = new File(path + "/Huai/TicketsPic");
      if(!file.exists())
      {
       file.mkdirs();
      }
      PicName = new File(file, imageName);
      try {
       if(!PicName.exists())
       {
        PicName.createNewFile();
       }
    FileOutputStream fos = new FileOutputStream(PicName);
    if(PicName.getName().endsWith(".png"))
    {
     bitmap.compress(CompressFormat.PNG, 100, fos);
    }
    else if(PicName.getName().endsWith(".jpg"))
    {
     bitmap.compress(CompressFormat.JPEG, 100, fos);
    }
    fos.flush();
    fos.close();
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   } catch (IOException e) {
    e.printStackTrace();
   }     
     }
    }

//从SD卡取
    public static Bitmap getBitmap(String imageName)
    {
     Bitmap bitmap = null;
     File imagePic;
     if(sdState.equals(Environment.MEDIA_MOUNTED))
     {
      
      imagePic = new File(path + "/Huai/TicketsPic", imageName);
      if(imagePic.exists())
      {
       try {
        bitmap = BitmapFactory.decodeStream(new FileInputStream(imagePic));
       } catch (FileNotFoundException e) {
//        e.printStackTrace();
       }
      }
     }
  return bitmap;
    }
    //将SD卡文件删除
    public static void  deleteFile(File file)
    {
     if(sdState.equals(Environment.MEDIA_MOUNTED))
     {
      if (file.exists())
      {
       if (file.isFile())
       {
        file.delete();
       }
       // 如果它是一个目录
       else if (file.isDirectory())
       {
        // 声明目录下所有的文件 files[];
        File files[] = file.listFiles();
        for (int i = 0; i < files.length; i++)
        { // 遍历目录下所有的文件
         deleteFile(files[i]); // 把每个文件 用这个方法进行迭代
        }
       }
       file.delete();
      }
     }
    }

转载地址:http://blog.sina.com.cn/s/blog_a72ec20c01015rwj.html