在android中的ExternalStoragePublicDirectory中找不到创建的文件

时间:2017-05-20 23:03:48

标签: android sensor bufferedwriter

我正在开发一个应用程序,它记录智能手机的加速度数据,并在每次点击开始录制按钮时将其保存到文件中, 但是我找不到创建的文件,尽管没有例外。

我在清单文件中添加了权限请求<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />,我检查了与此主题相关的先前问题,但我仍然无法找到问题所在?

以下是代码:

public class MainActivity extends AppCompatActivity implements SensorEventListener {

    int clicknumber = 0;
    private static final String LOG_TAG ="";
    private SensorManager mSensorManager;
    private Sensor mAccelerometer;
    private Button mstartButton= null;
    private boolean servicestatus = false;
    private RelativeLayout Rl = null;
    private LinearLayout ll= null;
    long timeOffsetMs= System.currentTimeMillis()-System.nanoTime() / 1000000;

    File file=null;
    BufferedWriter out = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        final String path = Environment.getExternalStoragePublicDirectory
                (Environment.DIRECTORY_DOCUMENTS).getPath();

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
        mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        servicestatus = isMyServiceRunning(RecordService.class);
        mstartButton = (Button) findViewById(R.id.recordButton);
        mstartButton.setText("Start Recording");
        mstartButton.setTextColor(Color.GREEN);
        Rl= (RelativeLayout) findViewById(R.id.Rlayout);
        ll = new LinearLayout(this);
        ll.setOrientation(LinearLayout.VERTICAL);
        Rl.addView(ll);
        final Intent service = new Intent(this, RecordService.class);
        mstartButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                clicknumber+=1;
                if (clicknumber % 2 != 0 || clicknumber == 0  ){
                    startService(service);
                    mstartButton.setText("Stop Recording");
                    mstartButton.setTextColor(Color.RED);
                    String newpath = path +"data.txt";
                    file = new File(newpath);

                }
                else{
                    stopService(service);
                    mstartButton.setText("Start Recording");
                    mstartButton.setTextColor(Color.GREEN);
                }
      }
        });
    }
    protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, mAccelerometer, SensorManager.SENSOR_DELAY_UI);

    }
    protected void onPause() {
        super.onPause();
        mSensorManager.unregisterListener(this);

    }
    @Override
    public void onSensorChanged(SensorEvent sensorEvent) {

        Sensor mySensor=sensorEvent.sensor;
        if (mySensor.getType()==Sensor.TYPE_ACCELEROMETER){
            float x=sensorEvent.values[0];
            float y=sensorEvent.values[1];
            float z=sensorEvent.values[2];
            long  timestamp=timeOffsetMs+ sensorEvent.timestamp / 1000000;
            if (isMyServiceRunning(RecordService.class) == true){
                TextView tv = new TextView(this);
                tv.setTextColor(Color.WHITE);
                tv.setText(String.format("t=%d,x=%.1f,y=%.1f,z=%.1f", timestamp,x,y,z));
                ll.addView(tv);
                try
                {
                    BufferedWriter out = new BufferedWriter(new FileWriter(file));
                    out.write(Float.toString(x) + Float.toString(y) +
                            Float.toString(z) );
                    out.close();
                }
                catch (IOException e)
                {
                    Log.e(MainActivity.LOG_TAG ,"Exception");
                }


            }
        }
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int i) {}



    private boolean isMyServiceRunning(Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service :
                manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}

提前致谢!

1 个答案:

答案 0 :(得分:0)

您的文件位于:/storage/emulated/0/Documentsdata.txt

请注意,应该满足2项要求,因为此处的错误并非意图。

  1. 目录文档必须存在。
  2. 您在以下行中忘记了/ 错误:字符串newpath = path +&#34; data.txt&#34; 无错误:字符串newpath =路径+&#34; /&#34; +&#34; data.txt&#34;
  3. 如果您现在测试代码,可以获取文件或抛出异常。

    你正在使用哪种api?声明清单中用于将文件写入存储的权限是必要的。但是对于completness,如果系统api> gt,你应该从用户那里获得许可。 23。