咖啡动力 发表于 2013-1-30 04:27:59

Android关于读取临时文件

  // 临时文件名
  final String tmpFileName = "comictmpsound.wav"; //Integer.toString(num++) + "." + type.substring(type.length() - 3, type.length());
  app.deleteFile(tmpFileName);
  
  FileOutputStream fos = null;
  
  try {
   fos = app.openFileOutput(tmpFileName, Context.MODE_PRIVATE);
   
   byte[] b = new byte;
   int offset = 0;
   int read = 0;
   do {
    read = ins.read(b);
    if (read > 0) {
     fos.write(b, 0, read);
     offset += read;
    }
   } while (read > 0);
   fos.flush();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (fos != null) {
    try {
     fos.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  
  // 打开临时文件
  FileInputStream fis = null;
  try {
   fis = app.openFileInput(tmpFileName);
   mp = new MediaPlayer();
   mp.setDataSource(fis.getFD());
   mp.prepare();
  } catch (Exception e) {
   e.printStackTrace();
  } finally {
   if (fis != null) {
    try {
     fis.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
页: [1]
查看完整版本: Android关于读取临时文件