无法从SD卡AVD模拟器Android中读取txt文件

时间:2015-11-05 16:40:43

标签: android

我试图从sd读取文件,但我不能。 这是我的代码。

我的清单:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/MyMaterialTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion="18" />

我的阅读文件类:

public ParsingFromTxt(String nomeFile, ArrayList<Squadra> squadre) throws IOException
{
    //Find the directory for the SD Card using the API
    //*Don't* hardcode "/sdcard"
    File sdcard = Environment.getExternalStorageDirectory();

    //Get the text file
    File file = new File(sdcard, "07-01.txt");
    if(file.exists())
        Log.d("log", "esiste");
    Log.d("log", file.getAbsolutePath());
    try
    {
        BufferedReader br = new BufferedReader(new FileReader(file));
        String line;
        Log.d("log", br.readLine());
        while ((line = br.readLine()) != null) 
        { ...
        }
    }
    catch(Exception exc)
    {
         Log.d("log", "errore"); 
    }

这是我在控制台中读到的内容:

11-05 16:32:25.980 2144-2144/prova.myapplication D/log: esiste
11-05 16:32:25.980 2144-2144/prova.myapplication D/log: /storage/12EA-101E/07-01.txt
11-05 16:32:25.980 2144-2144/prova.myapplication D/log: Errore

帮我查找错误。

2 个答案:

答案 0 :(得分:0)

我认为您需要为清单文件添加一个权限。

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

答案 1 :(得分:0)

它是否适用于真实设备?

您的AndroidManifest.xml中是否有<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />权限?