在 android 的 ExternalStoragePublicDirectory 中找不到创建的文件
Can't find the created file in the ExternalStoragePublicDirectory in android
我正在开发一个应用程序,它可以记录来自智能手机的加速度数据,并在我每次点击开始记录按钮时将它们保存到一个文件中,
但是我找不到创建的文件,尽管没有出现异常。
我在清单文件中添加了权限请求 <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;
}
}
提前致谢!
您的文件位于:/storage/emulated/0/Documentsdata.txt
注意2点要满足,因为这里的错误不是故意的。
- 目录文档必须存在。
- 您在下一行中忘记了
/
错误:字符串新路径=路径+"data.txt"
没有错误 : String newpath = path +"/"+"data.txt"
如果您现在测试您的代码,您可以获得文件或抛出异常。
您正在使用哪个 api?。在清单中声明将文件写入存储的权限是必要的。但为了完整起见,如果系统 api > 23.
,您应该已授予用户许可
我正在开发一个应用程序,它可以记录来自智能手机的加速度数据,并在我每次点击开始记录按钮时将它们保存到一个文件中, 但是我找不到创建的文件,尽管没有出现异常。
我在清单文件中添加了权限请求 <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;
}
}
提前致谢!
您的文件位于:/storage/emulated/0/Documentsdata.txt
注意2点要满足,因为这里的错误不是故意的。
- 目录文档必须存在。
- 您在下一行中忘记了
/
错误:字符串新路径=路径+"data.txt" 没有错误 : String newpath = path +"/"+"data.txt"
如果您现在测试您的代码,您可以获得文件或抛出异常。
您正在使用哪个 api?。在清单中声明将文件写入存储的权限是必要的。但为了完整起见,如果系统 api > 23.
,您应该已授予用户许可