Android MediaRecorder and setOutputFile

荒凉一梦 提交于 2019-12-03 13:50:21

You can of course read the file in later and do whatever you want with it in the way of processing. Assuming that u holds the Uri to the resulting audio file, here is a code snippet that reads it into a byte array and then deletes the file.

String audioUri = u.getPath();
InputStream in = new BufferedInputStream(this.getContentResolver().openInputStream(u));
byte[] b = new byte[BUFSIZE];

BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(new File(mFileName/*mFilePath*/)));
int byteCnt = 0;
while (0 <= (byteCnt = in.read(b, 0, BUFSIZE)))
   out.write(b, 0, byteCnt);
out.flush();
out.close();
// try to delete media file
try {
   // Delete media file pointed to by Uri
   new File(getRealPathFromURI(u)).delete();
} catch (Exception ex) {}


   public String getRealPathFromURI(Uri contentUri) {
      String[] proj = { MediaStore.Images.Media.DATA };
      Cursor cursor = managedQuery(contentUri, proj, null, null, null);
      int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      cursor.moveToFirst();
      return cursor.getString(column_index);
   }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!