我有以下代码用于将音频峰值和加速度计读数记录到文件中。除了没有创建或写入文件外,一切似乎都工作正常。我似乎无法弄明白。
非常感谢任何帮助。
public class Main extends Activity implements SensorEventListener {
private SensorManager mSensorManager;
private Sensor mAccelerometer;
private MediaRecorder recorder = new MediaRecorder();
private File accel;
private File audio;
private File audiodump;
private FileWriter accelwriter;
private FileWriter audiowriter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
accel = new File(root,"accel.txt");
audio = new File(root,"audio.txt");
audiodump = new File(root,"audiodump.aac");
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
recorder.setOutputFile(audiodump.getPath());
setContentView(R.layout.activity_main);
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
}
public void onStartClick(View view) {
mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_NORMAL);
}
public void onStopClick(View view) {
mSensorManager.unregisterListener(this);
}
protected void onResume() {
super.onResume();
try {
accelwriter = new FileWriter(accel, true);
audiowriter = new FileWriter(audio, true);
recorder.prepare();
recorder.start();
} catch(IOException e) {
e.printStackTrace();
}
}
protected void onPause() {
super.onPause();
try {
recorder.stop();
recorder.release();
if (accelwriter != null) {
accelwriter.close();
}
if (audiowriter!= null) {
audiowriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
String time = String.valueOf(System.currentTimeMillis());
String amplitude = String.valueOf(recorder.getMaxAmplitude());
float x = event.values[0];
float y = event.values[1];
float z = event.values[2];
try {
audiowriter.write(time+amplitude+"\n");
accelwriter.write(time+x+","+y+","+z+"\n");
} catch (IOException e) {
e.printStackTrace();
}
}
}
这是清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ross.pieter.pieterfinal">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是logcat:
05-24 09:22:19.924 3229-3229/ross.pieter.pieterfinal W/System: ClassLoader referenced unknown path: /data/app/ross.pieter.pieterfinal-1/lib/x86
05-24 09:22:20.105 3229-3264/ross.pieter.pieterfinal D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
05-24 09:22:20.151 3229-3264/ross.pieter.pieterfinal I/OpenGLRenderer: Initialized EGL, version 1.4
05-24 09:22:55.116 3229-3229/ross.pieter.pieterfinal W/MediaRecorder: mediarecorder went away with unhandled events
05-24 09:22:55.329 3229-3264/ross.pieter.pieterfinal E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4053500
05-24 09:23:30.989 3229-3235/ross.pieter.pieterfinal W/art: Suspending all threads took: 5.202ms
05-24 09:29:04.529 32274-32274/ross.pieter.pieterfinal I/art: Late-enabling -Xcheck:jni
05-24 09:29:04.529 32274-32274/ross.pieter.pieterfinal I/art: VMHOOK: rlim_cur : 0 pid:32274
05-24 09:29:04.603 32274-32274/ross.pieter.pieterfinal W/System: ClassLoader referenced unknown path: /data/app/ross.pieter.pieterfinal-2/lib/arm
05-24 09:29:05.120 32274-32274/ross.pieter.pieterfinal W/System: ClassLoader referenced unknown path: /data/app/ross.pieter.pieterfinal-2/lib/arm
05-24 09:29:05.220 32274-32274/ross.pieter.pieterfinal D/MediaRecorder: Current Package Name: ross.pieter.pieterfinal
05-24 09:29:05.259 32274-32274/ross.pieter.pieterfinal E/SensorManager: uid = 10042
05-24 09:29:05.261 32274-32274/ross.pieter.pieterfinal D/MediaRecorder: getMaxAllowedFileSizeByPath
05-24 09:29:05.263 32274-32274/ross.pieter.pieterfinal D/MediaRecorder: no match storage volume found!
05-24 09:29:05.382 32274-32324/ross.pieter.pieterfinal I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: EGL 1.4 QUALCOMM build: Nondeterministic_AU_msm8974_LA.BF.1.1.3__release_AU (I3fa967cfef)
OpenGL ES Shader Compiler Version: E031.28.00.02
Build Date: 09/29/15 Tue
Local Branch: mybranch14683032
Remote Branch: quic/master
Local Patches: NONE
Reconstruct Branch: NOTHING
05-24 09:29:06.633 32274-32274/ross.pieter.pieterfinal I/SensorManager: registerListenerImpl: listener = ross.pieter.pieterfinal.Main@2e56edc, sensor = {Sensor name="Accelerometer Sensor", vendor="HTC Group Ltd.", version=1, type=1, maxRange=19.6133, resolution=0.01, power=0.17, minDelay=10000}, delay = 200000, handler = null
05-24 09:29:09.490 32274-32274/ross.pieter.pieterfinal I/SensorManager: unregisterListenerImpl++: listener = ross.pieter.pieterfinal.Main@2e56edc
05-24 09:29:10.367 32274-32274/ross.pieter.pieterfinal I/SensorManager: registerListenerImpl: listener = ross.pieter.pieterfinal.Main@2e56edc, sensor = {Sensor name="Accelerometer Sensor", vendor="HTC Group Ltd.", version=1, type=1, maxRange=19.6133, resolution=0.01, power=0.17, minDelay=10000}, delay = 200000, handler = null
05-24 09:29:12.437 32274-32274/ross.pieter.pieterfinal I/SensorManager: unregisterListenerImpl++: listener = ross.pieter.pieterfinal.Main@2e56edc
05-24 09:29:12.809 32274-32274/ross.pieter.pieterfinal I/SensorManager: registerListenerImpl: listener = ross.pieter.pieterfinal.Main@2e56edc, sensor = {Sensor name="Accelerometer Sensor", vendor="HTC Group Ltd.", version=1, type=1, maxRange=19.6133, resolution=0.01, power=0.17, minDelay=10000}, delay = 200000, handler = null
05-24 09:29:13.215 32274-32274/ross.pieter.pieterfinal I/SensorManager: unregisterListenerImpl++: listener = ross.pieter.pieterfinal.Main@2e56edc
05-24 09:29:13.614 32274-32274/ross.pieter.pieterfinal I/SensorManager: registerListenerImpl: listener = ross.pieter.pieterfinal.Main@2e56edc, sensor = {Sensor name="Accelerometer Sensor", vendor="HTC Group Ltd.", version=1, type=1, maxRange=19.6133, resolution=0.01, power=0.17, minDelay=10000}, delay = 200000, handler = null
05-24 09:29:13.986 32274-32274/ross.pieter.pieterfinal I/SensorManager: unregisterListenerImpl++: listener = ross.pieter.pieterfinal.Main@2e56edc
05-24 09:29:14.340 32274-32274/ross.pieter.pieterfinal I/SensorManager: registerListenerImpl: listener = ross.pieter.pieterfinal.Main@2e56edc, sensor = {Sensor name="Accelerometer Sensor", vendor="HTC Group Ltd.", version=1, type=1, maxRange=19.6133, resolution=0.01, power=0.17, minDelay=10000}, delay = 200000, handler = null
答案 0 :(得分:0)
原来代码很好。
在重新启动之前,文件可以通过某些设备上的MTP连接保持不可见
在这里找到答案:
https://android.stackexchange.com/questions/46315/not-all-files-are-visible-over-mtp