您好,谢谢您的帮助。我正在尝试读取我的android上的文件并在按下按钮时将其放入ArrayList但是我在标题中收到错误并将在下面发布确切的代码。
MainActivity.Java
package com.teamfara.circadianrhythmmonitor2;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.Set;
import java.util.UUID;
public class MainActivity extends AppCompatActivity {
/* MAIN GOALS
- Receive bodyTemp txt file and convert into array
- Send light info as txt file
- put bodyTemp into current graph
- make targeted body temp graph
*/
private final static int REQUEST_ENABLE_BT = 1;
UUID myUUID = UUID.randomUUID();
private static final int DISCOVER_DURATION = 300;
private static final int REQUEST_BLU = 1;
ArrayAdapter<String> mArrayAdapter;
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
Intent enableBIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
startActivityForResult(enableBIntent, REQUEST_ENABLE_BT);
}
//If there are paired devices
/*Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
if (pairedDevices.size() > 0) {
//Loop through the paired devices
for (BluetoothDevice device : pairedDevices) {
//add the name and address to an array adapter to show in a ListView
mArrayAdapter.add(device.getName() + "\n" + device.getAddress());
}
}
*/
IntentFilter filter = new IntentFilter(BluetoothDevice.ACTION_FOUND);
registerReceiver(mReceiver, filter); // Don't forget to unregister during onDestroy
Intent discoverableIntent = new
Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
discoverableIntent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
startActivity(discoverableIntent);
}
public void receiveBodyTempInfo(View view) throws FileNotFoundException{
ArrayList<Double> list = new ArrayList<>();
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
//Read text from file
Scanner file2 = new Scanner(new File(sdcard, "temp.txt"));
while(file2.hasNextLine()) {
String line = file2.nextLine();
Scanner scanner = new Scanner(line);
scanner.useDelimiter(",");
while(scanner.hasNextDouble()) {
list.add(scanner.nextDouble());
}
scanner.close();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src = "@drawable/flight2"
android:scaleType = "centerCrop"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation = "vertical"
android:padding = "16sp"
>
<!-- Top three buttons-->
<LinearLayout
android:orientation = "horizontal"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- Light-->
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src = "@drawable/light2"
android:paddingBottom = "8sp"
/>
<!-- Body Temp-->
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src = "@drawable/temperature"
android:paddingBottom = "8sp"/>
<!-- Heart Rate-->
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:src = "@drawable/heart"/>
</LinearLayout>
<!-- Current Trip-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight = "1"
android:orientation = "vertical"
android:background="#CC0099FF"
>
<!-- Exact Trip-->
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="2"
android:text = "New York -> London"
android:textAllCaps = "true"
android:textSize = "24sp"
android:layout_gravity = "center_horizontal"
/>
<!-- Be Awake...-->
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Wake up at "
android:layout_gravity = "left"/>
<!-- Be in Light...
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Be in Light today from 1PM-1:30AM"
android:layout_gravity = "left"/>
-->
<!-- Eat...
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Eat today at 1PM, 6PM, 11:30PM"
android:layout_gravity = "left"/>
-->
<!-- Be in Dark...
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Be in the Dark from 1:30PM to Bed"
android:layout_gravity = "left"/>
-->
<!-- Sleep...-->
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Go to Sleep at "
android:layout_gravity = "left"
android:id = "@+id/text_view"
/>
</LinearLayout>
<!-- Miscellaneous-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight = "2"
android:orientation = "vertical">
<!-- Add Trip-->
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Add Trip"/>
<!-- Connect Bluetooth-->
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Send Light Information"
android:id = "@+id/sendLightInfo"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text = "Receive Body Temperature Information"
android:id = "@+id/receiveBodyTempInfo"
android:onClick = "receiveBodyTempInfo"
/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.teamfara.circadianrhythmmonitor2" >
<!-- Allows bluetooth to be used from this app -->
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name = "android.permission.BLUETOOTH_ADMIN"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
错误消息
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 D/AndroidRuntime: Shutting down VM
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: FATAL EXCEPTION: main
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: Process: com.teamfara.circadianrhythmmonitor2, PID: 29694
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: java.lang.IllegalStateException: Could not execute method of the activity
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$1.onClick(View.java:4298)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View.performClick(View.java:5254)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21179)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: Caused by: java.lang.reflect.InvocationTargetException
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$1.onClick(View.java:4293)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View.performClick(View.java:5254)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21179)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: Caused by: java.io.FileNotFoundException: /storage/emulated/0/temp.txt: open failed: EACCES (Permission denied)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at libcore.io.IoBridge.open(IoBridge.java:456)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.io.FileInputStream.<init>(FileInputStream.java:76)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.util.Scanner.<init>(Scanner.java:158)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.util.Scanner.<init>(Scanner.java:138)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.teamfara.circadianrhythmmonitor2.MainActivity.receiveBodyTempInfo(MainActivity.java:57)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$1.onClick(View.java:4293)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View.performClick(View.java:5254)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21179)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at libcore.io.Posix.open(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at libcore.io.IoBridge.open(IoBridge.java:442)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.io.FileInputStream.<init>(FileInputStream.java:76)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.util.Scanner.<init>(Scanner.java:158)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.util.Scanner.<init>(Scanner.java:138)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.teamfara.circadianrhythmmonitor2.MainActivity.receiveBodyTempInfo(MainActivity.java:57)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$1.onClick(View.java:4293)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View.performClick(View.java:5254)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:21179)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:739)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:95)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.os.Looper.loop(Looper.java:145)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6837)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
10-23 21:33:55.398 29694-29694/com.teamfara.circadianrhythmmonitor2 E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
有人可以帮忙吗?
答案 0 :(得分:1)
堆栈跟踪很清楚:
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
如果您搜索此错误,则会找到this post。这基本上说你错过了一个权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
但是,您正在尝试阅读,因此您需要:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Google文档也say the same thing:
要在外部存储上读取或写入文件,您的应用必须获取READ_EXTERNAL_STORAGE