0
爲了讓我的應用程序運行,它需要打開一個txt文件,我將它保存到手機內存中,並使用其中的數據。打開文件失敗,完成清單中的權限
我已經將清單中的權限放在應用程序之上,並且已經把我認爲是正確的路徑。該程序無法運行,並給出了logcat中
java.io.FileNotFoundException:/storage/TestDaten/input.txt:打開失敗:ENOENT(沒有這樣的文件或目錄)
這是一些碼。
在清單:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE_"></uses-permission>
在MainActivity
public class MainActivity extends AppCompatActivity {
// Storage Permissions
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
// Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
AudioAnalyzer aa = new AudioAnalyzer();
Button button;
private View.OnClickListener runfilter = new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
double[] input = readFile("/storage/TestDaten/input.txt");
int i;
double inputd = input[0];
for (i=0;i<input.length-1; i++){
inputd = input[i];
}
isDetected(aa.EventAnalyzer(inputd));
}catch (IOException e) {
e.printStackTrace();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.buttonrun);
button.setOnClickListener(runfilter);
verifyStoragePermissions(this);
}
public double[] readFile(String fname) throws FileNotFoundException{
File file = new File(fname);
Scanner scanner = new Scanner(new FileInputStream(file));
List<Double> collection = new ArrayList<>();
while (scanner.hasNextDouble())
{
double number = scanner.nextDouble();
collection.add(number);
}
scanner.close();
return toPrimitive(collection.toArray(new Double[collection.size()]));
}
public double[] toPrimitive(Double[] array) {
if (array == null) {
return null;
} else if (array.length == 0) {
return null;
}
final double[] result = new double[array.length];
for (int i = 0; i < array.length; i++) {
result[i] = array[i].doubleValue();
}
return result;}
private void isDetected(int a){
Context context = getApplicationContext();
CharSequence t0 = "No breathing";
CharSequence t1 = "Normal breathing";
CharSequence t2 = "Snoring";
CharSequence t3 = "Apnea!";
int duration = Toast.LENGTH_SHORT;
Toast toast0 = Toast.makeText(context, t0, duration);
Toast toast1 = Toast.makeText(context, t1, duration);
Toast toast2 = Toast.makeText(context, t2, duration);
Toast toast3 = Toast.makeText(context, t3, duration);
if(a==0)
toast0.show();
if(a==1)
toast1.show();
if(a==2)
toast2.show();
if(a==3)
toast3.show();
}
}
感謝您的建議。但不是查看內容,我只是想使用文件內容進行計算。我需要使用保存的值來模擬實時值。 –