File存储—外部存储

2、外部存储,sdcard
   案例一:外部存储时一般通用的工具类
   public class SDCardUtils {
 // 判断SDCard是否挂载,true,挂载,false,未挂载
 // Environment.MEDIA_MOUNTED,表示sdCard已经挂载
 // Environment.getExternalStorageState(),获得当前sdCard的挂载状态
 public static boolean isMounted() {
  if (Environment.MEDIA_MOUNTED.equals(Environment
    .getExternalStorageState())) {
   return true;
  }
  return false;
 }

 // 获得SDCard 的路径
 public static String getSDCardPath() {
  if (isMounted()) {
   return Environment.getExternalStorageDirectory().getAbsolutePath();
  }
  return null;
 }

 // 获得scCard的大小
 public static long getSize() {
  if (isMounted()) {
   // StatFs,用来计算文件系统存储空间大小的工具类
   StatFs stat = new StatFs(getSDCardPath());

   // 获得块的数量
   // int count = stat.getBlockCount();
   long count = stat.getBlockCountLong();
   // 获得每块的大小
   // int size = stat.getBlockSize();
   long size = stat.getBlockSizeLong();

   return count * size / 1024 / 1024;
  }
  return 0;
 }

 // 获得sdcard可用大小
 public static long getAvailableSize() {
  if (isMounted()) {
   StatFs stat = new StatFs(getSDCardPath());
   // 可用的块数
   long availCount = stat.getAvailableBlocksLong();
   // 块的大小
   long size = stat.getBlockSizeLong();
   return availCount * size / 1024 / 1024;
  }
  return 0;
 }

 // 将文件保存到sdCard
 // data,保存的数据
 // dir,保存的路径
 // fileName,保存的文件名
 public static boolean saveDataIntoSDCard(byte[] data, String dir,
   String fileName) {
  if (isMounted()) {
   // 先判断存储的路径dir是否存在(创建),如果不存在,先创建
   String path = getSDCardPath() + File.separator + dir;
   File file = new File(path);
   if (!file.exists()) {
    file.mkdirs();
   }

   // 如果存在,进行读写操作
   file = new File(path + File.separator + fileName);
   BufferedOutputStream bos = null;
   try {
    bos = new BufferedOutputStream(new FileOutputStream(file));
    bos.write(data);
    bos.flush();

    return true;
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } finally {
    try {
     if (bos != null)
      bos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  return false;
 }

 // 读取sdcard的数据
 public static byte[] getDataFromSDCard(String dir, String fileName) {
  if (isMounted()) {
   String path = getSDCardPath() + File.separator + dir
     + File.separator + fileName;
   File file = new File(path);
   if (file.exists()) {
    ByteArrayOutputStream baos = null;
    BufferedInputStream bis = null;

    try {
     baos = new ByteArrayOutputStream();
     bis = new BufferedInputStream(new FileInputStream(file));

     byte[] b = new byte[1024 * 8];
     int n = 0;
     while ((n = bis.read(b)) != -1) {
      baos.write(b, 0, n);
      baos.flush();
     }
     return baos.toByteArray();
    } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  return null;
 }

 // 根据具体的类型获得公共路径
 public static String getPublicPath(String type) {
  if (isMounted()) {
   return Environment.getExternalStoragePublicDirectory(type)
     .getAbsolutePath();
  }
  return null;
 }

 // 保存公共路径下
 public static boolean saveDataIntoSDCardPublic(byte[] data, String type,
   String fileName) {
  if (isMounted()) {
   File file = new File(getPublicPath(type));
   if (!file.exists()) {
    file.mkdirs();
   }
   BufferedOutputStream bos = null;
   try {
    bos = new BufferedOutputStream(new FileOutputStream(new File(
      file, fileName)));
    bos.write(data, 0, data.length);
    bos.flush();

    return true;
   } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } finally {
    try {
     if (bos != null)
      bos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  return false;
 }

 // 根据上下文,以及路径的类型,返回私有的路径名
 public static String getPrivatePath(Context context, String type) {
  if (isMounted()) {
   return context.getExternalFilesDir(type).getAbsolutePath();
  }
  return null;
 }

 // 保存到私有路径下
 public static boolean saveDataIntoSDCardPrivate(byte[] data,
   Context context, String type, String fileName) {
  if (isMounted()) {
   File file = new File(getPrivatePath(context, type));
   if (!file.exists()) {
    file.mkdirs();
   }

   BufferedOutputStream bos = null;
   try {
    bos = new BufferedOutputStream(new FileOutputStream(new File(file, fileName)));
    bos.write(data, 0, data.length);
    bos.flush();
    return true;

   } catch (Exception e) {
    e.printStackTrace();
   } finally {
    try {
     if (bos != null)
      bos.close();
    } catch (IOException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  return false;
 }
}
案例二:文件浏览器

acctivity中:
public class MainActivity extends Activity {

 private TextView tvCurrent, tvState;
 private ListView lv;

 private FileAdapter adapter;

 private boolean flag;

 // 存储数据:包括文件名(文件夹名) 和 图片
 private List<Map<String, String>> data = new ArrayList<Map<String, String>>();

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  initView();
  // 使用工具类,获得LIstView中显示的数据
  if (SDCardUtils.isMounted()) {
   flag = SDCardUtils.listDirectory(new File(SDCardUtils.getSDCardPat()), data);
  }
  // 设置当前路径
  tvCurrent.setText(SDCardUtils.getSDCardPath());
  // 显示存储状态
  long size = SDCardUtils.getSize();
  long availSize = SDCardUtils.getAvailableSize();
  tvState.setText("总容量:" + size + ",可用容量:" + availSize);

  // 创建并设置适配器
  adapter = new FileAdapter(this, data);
  lv.setAdapter(adapter);
 }

 private void initView() {
  // TODO Auto-generated method stub
  tvCurrent = (TextView) findViewById(R.id.tv_current);
  tvState = (TextView) findViewById(R.id.tv_state);
  lv = (ListView) findViewById(R.id.lv);

  // @android:id/empty",如果是ListActivity,自动添加
  // 如果是普通的Activity,使用该方法添加空视图
  lv.setEmptyView(findViewById(android.R.id.empty));

  // Listview绑定单击事件,点击,进入下一级
  lv.setOnItemClickListener(new OnItemClickListener() {

   @Override
   public void onItemClick(AdapterView<?> parent, View view,
     int position, long id) {
    // TODO Auto-generated method stub
    // 组织下一级的路径名,有可能是文件名
    String clickPath = tvCurrent.getText().toString()
      + File.separator + data.get(position).get("name");

    File file = new File(clickPath);
    // 判断该路径是否为文件夹
    if (file.isDirectory()) {
     boolean b = SDCardUtils.listDirectory(file, data);
     if (b) {
      adapter.notifyDataSetChanged();
      tvCurrent.setText(clickPath);
     } else {
      Toast.makeText(getApplicationContext(), "文件夹不可访问", 1)
        .show();
     }
    } else {
     // 不同类型的文件使用隐式意图打开
    }

   }
  });
 }

 // 返回上一级按钮
 public void back(View v) {
  // 获得当前路径
  String current = tvCurrent.getText().toString();

  // 判断是否到达根目录
  if (current.equals(SDCardUtils.getSDCardPath())) {
   Toast.makeText(this, "已经到达根目录", 1).show();
  } else {
   File file = new File(current);
   // 获得父目录,上一级
   File parentFile = file.getParentFile();

   // 根据上一级目录,获得内容,并显示
   SDCardUtils.listDirectory(parentFile, data);
   adapter.notifyDataSetChanged();
   tvCurrent.setText(parentFile.getAbsolutePath());
  }
 }
}
适配器:
public class FileAdapter extends BaseAdapter {

 private Context context;
 private List<Map<String, String>> data;

 public FileAdapter(Context context, List<Map<String, String>> data) {
  this.context = context;
  this.data = data;
 }

 @Override
 public int getCount() {
  // TODO Auto-generated method stub
  return data.size();
 }

 @Override
 public Object getItem(int position) {
  // TODO Auto-generated method stub
  return data.get(position);
 }

 @Override
 public long getItemId(int position) {
  // TODO Auto-generated method stub
  return position;
 }

 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
  // TODO Auto-generated method stub
  ViewHolder holder;
  if (convertView == null) {
   holder = new ViewHolder();
   convertView = LayoutInflater.from(context).inflate(R.layout.item,
     null);
   holder.iv = (ImageView) convertView.findViewById(R.id.iv);
   holder.tv = (TextView) convertView.findViewById(R.id.tv);
   convertView.setTag(holder);
  } else {
   holder = (ViewHolder) convertView.getTag();
  }

  Map<String, String> map = data.get(position);
  holder.tv.setText(map.get("name").toString());

  String type = map.get("type").toString();
  int resId = 0;
  if ("dir".equals(type)) {
   resId = R.drawable.greenpng_016;
  } else if ("file".equals(type)) {
   resId = R.drawable.greenpng_011;
  }
  holder.iv.setImageResource(resId);

  return convertView;
 }

 class ViewHolder {
  TextView tv;
  ImageView iv;
 }
}
外部存储的工具类中再添加一个方法:
        // 遍历某个文件夹下的内容
 // file:文件夹名
 // data:数据的集合
 public static boolean listDirectory(File file,
   List<Map<String, String>> data) {

  // 遍历文件夹下的内容
  File[] files = file.listFiles();
  if (files == null) {// 文件夹为空
   return false;
  }
  // 处理文件夹不为空

  // 清空之前存储的数据
  data.clear();

  for (File f : files) {
   Map<String, String> map = new HashMap<String, String>();
   String fileName = f.getName();// 获得文件名

   String type = "";// 类型,表示文件夹或文件
   if (f.isDirectory()) {
    type = "dir";
   } else if (f.isFile()) {
    type = "file";
   }

   map.put("name", fileName);
   map.put("type", type);
   data.add(map);

  }
  return true;

 }
布局文件中和菜单中的省略。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在信号处理领域,DOA(Direction of Arrival)估计是一项关键技术,主要用于确定多个信号源到达接收阵列的方向。本文将详细探讨三种ESPRIT(Estimation of Signal Parameters via Rotational Invariance Techniques)算法在DOA估计中的实现,以及它们在MATLAB环境中的具体应用。 ESPRIT算法是由Paul Kailath等人于1986年提出的,其核心思想是利用阵列数据的旋转不变性来估计信号源的角度。这种算法相比传统的 MUSIC(Multiple Signal Classification)算法具有较低的计算复杂度,且无需进行特征值分解,因此在实际应用中颇具优势。 1. 普通ESPRIT算法 普通ESPRIT算法分为两个主要步骤:构造等效旋转不变系统和估计角度。通过空间平移(如延时)构建两个子阵列,使得它们之间的关系具有旋转不变性。然后,通过对子阵列数据进行最小二乘拟合,可以得到信号源的角频率估计,进一步转换为DOA估计。 2. 常规ESPRIT算法实现 在描述中提到的`common_esprit_method1.m`和`common_esprit_method2.m`是两种不同的普通ESPRIT算法实现。它们可能在实现细节上略有差异,比如选择子阵列的方式、参数估计的策略等。MATLAB代码通常会包含预处理步骤(如数据归一化)、子阵列构造、旋转不变性矩阵的建立、最小二乘估计等部分。通过运行这两个文件,可以比较它们在估计精度和计算效率上的异同。 3. TLS_ESPRIT算法 TLS(Total Least Squares)ESPRIT是对普通ESPRIT的优化,它考虑了数据噪声的影响,提高了估计的稳健性。在TLS_ESPRIT算法中,不假设数据噪声是高斯白噪声,而是采用总最小二乘准则来拟合数据。这使得算法在噪声环境下表现更优。`TLS_esprit.m`文件应该包含了TLS_ESPRIT算法的完整实现,包括TLS估计的步骤和旋转不变性矩阵的改进处理。 在实际应用中,选择合适的ESPRIT变体取决于系统条件,例如噪声水平、信号质量以及计算资源。通过MATLAB实现,研究者和工程师可以方便地比较不同算法的效果,并根据需要进行调整和优化。同时,这些代码也为教学和学习DOA估计提供了一个直观的平台,有助于深入理解ESPRIT算法的工作原理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值