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


    //保存到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

  • 相关阅读:
    C# Process.Start()方法详解 .
    任务管理器
    20160113 js中选择多个check一块删除
    20160113 JS中CheckBox如何控制全选
    20151217JS便签
    20151216Repeater
    20151215单选按钮列表,复选框列表:CheckBoxList
    20151214下拉列表:DropDownList
    !!!SqlHelper 传智!
    !!! SQL 数据库开发基础 传智!
  • 原文地址:https://www.cnblogs.com/dyllove98/p/3165584.html
Copyright © 2020-2023  润新知