【发布时间】:2017-09-05 19:57:33
【问题描述】:
我试图在文本文件中捕获传感器数据,当我通过 USB 将手机连接到 PC 时,我可以从手机的“外部存储”中提取这些数据,这样我就可以通过 python 脚本运行文本文件以进行绘图。我还没有实现传感器部分,因为我无法创建可以在创建后从手机复制到 PC 的文件。
我尝试创建一个文件夹,但它没有创建文件夹,我还尝试创建一个文件,它也失败了,然后它抛出一个文件不存在的异常。我不知所措,因为我已将权限添加到清单文件中,如下所示。有什么我想念的吗?我已经做了很多搜索,似乎没有任何解决方案可以解决我的问题,并且 developer.android.com 网站除了添加我已经添加到清单文件中的行之外没有解释任何有关权限的内容。
我没有 SD 卡,因此使用 sd 卡无法解决我的问题。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mike.project_final">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我到目前为止所做的全部代码,它会引发错误。
package com.example.mike.project_final;
import android.content.Context;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.hardware.Sensor;
import android.view.View;
import android.widget.TextView;
import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
public class MainActivity extends AppCompatActivity implements SensorEventListener {
TextView xTV, yTV, zTV, writeTV;
SensorManager sManager;
Sensor sAccelerometer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
xTV = (TextView) findViewById(R.id.xTV);
yTV = (TextView) findViewById(R.id.yTV);
zTV = (TextView) findViewById(R.id.zTV);
writeTV = (TextView) findViewById(R.id.writeTV);
}
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
public void createFile(View view) {
File root = android.os.Environment.getExternalStorageDirectory();
File dir = new File (root.getAbsolutePath() + "/project_test");
if (!dir.exists()){
dir.mkdir();
xTV.setText("Created");
}
else{
xTV.setText("Exists");
}
File file = new File(dir, "myData.txt");
try {
FileOutputStream f = new FileOutputStream(file);
PrintWriter pw = new PrintWriter(f);
pw.println("Hi , How are you");
pw.println("Hello");
pw.flush();
pw.close();
f.close();
} catch (Exception e) {
writeTV.setText(e.toString());
}
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy){
//do nothing
}
@Override
public void onSensorChanged(SensorEvent event) {
}
}
如果需要,任何有关如何设置其余权限的帮助将不胜感激!
编辑 1: 权限问题已解决,谢谢。我现在有一个问题,我创建的文件夹不是作为文件夹创建,而是作为文件创建,如下图所示。我到底做错了什么? https://i.imgur.com/srKXJc1g.png 不幸的是,我没有足够的声誉来发布图片。
【问题讨论】:
-
我设法让权限正常工作,但使用 mediascannerconnection 的整个过程让我无法接受。它会创建,当我尝试发现该文件夹时,它不会显示为文件夹,而是显示为 4kB 大小的空白白色文档?在这方面您可以提供什么建议?
-
在调用
MediaScannerConnection之前,请确保您已将文件完全写入磁盘。请参阅我的另一本不相关的书籍样本中的these lines。 -
dir.mkdir();。你哭了 Created 但你应该检查返回值,因为 mkdirs() 可能会失败。如果是这样,您应该返回。