Reading and writing a file from an SD card

I bought a book ("The Beginning of Android Games") by Mario Sechner. On page 149, he talks about saving and opening a file on external storage. I understand the code, however, I don’t understand WHY he says this:

Screenshothot of error

Why is this so? I have all the permissions in my manifest:

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.amzoft.android.reference" android:versionCode="1" android:versionName="1.0" android:installLocation="preferExternal"> <application android:label="Android Reference" android:icon="@drawable/ic_launcher" android:debuggable="true"> <activity android:label="Android Reference" android:name=".AndroidReferenceActivity" android:screenOrientation="portrait" android:configChanges="keyboard|keyboardHidden|orientation"> <intent-filter > <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:label="LifeCycleTest" android:name=".LifeCycleTest" android:configChanges="keyboard|keyboardHidden|orientation"/> <activity android:label="SingleTouchTest" android:name=".SingleTouchTest" android:configChanges="keyboard|keyboardHidden|orientation"/> <activity android:label="MultiTouchTest" android:name=".MultiTouchTest" android:configChanges="keyboard|keyboardHidden|orientation"/> <activity android:label="KeyTest" android:name=".KeyTest" android:configChanges="keyboard|keyboardHidden|orientation"/> <activity android:label="AccelerometerTest" android:name=".AccelerometerTest" android:configChanges="keyboard|keyboardHidden|orientation"/> <activity android:label="AssetsTest" android:name=".AssetsTest" android:configChanges="keyboard|keyboardHidden|orientation"/> <activity android:label="ExternalStorageTest" android:name=".ExternalStorageTest" android:configChanges="keyboard|keyboardHidden|orientation"/> </application> <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="10"/> <uses-permission android:name="android.permisson.WRITE_EXTERNAL_STORAGE" android:required="true"/> <uses-permission android:name="android.permission.WAKE_LOCK" android:required="false"/> 

And my code is:

 package com.amzoft.android.reference; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import android.app.Activity; import android.os.Bundle; import android.os.Environment; import android.widget.TextView; public class ExternalStorageTest extends Activity{ @Override public void onCreate(Bundle saveInstanceState) { super.onCreate(saveInstanceState); TextView textView = new TextView(this); setContentView(textView); String state = Environment.getExternalStorageState(); if(!state.equals(Environment.MEDIA_MOUNTED)) { textView.setText("SD card is not mounted"); }else{ File externalDir = Environment.getExternalStorageDirectory(); File textFile = new File(externalDir.getAbsolutePath() + File.separator + "text.txt"); try{ writeTextFile(textFile, "This is a test >:0\nLINE BREAK"); String text = readTextFile(textFile); textView.setText(text); if(!textFile.delete()) { textView.setText("Couldn't remove temporary directory, sorry mate."); } }catch(Exception e){ textView.setText(e.getMessage()); } } } private void writeTextFile(File file, String text) throws IOException { BufferedWriter writer = new BufferedWriter(new FileWriter(file)); writer.write(text); writer.close(); } private String readTextFile(File file) throws IOException { BufferedReader reader = new BufferedReader(new FileReader(file)); StringBuilder text = new StringBuilder(); String line; while((line = reader.readLine()) != null) { text.append(line); text.append("\n");//BECAUSE ITS A LINE :D } reader.close(); return text.toString(); } } 

I hope someone can help me because Im confused right now.

+4
source share
2 answers

I think his permission issue adds both of them to the manifest file

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

tell me how it turns out

+2
source

You seem to have the correct permissions, but you are missing the clossing manifest tag:

 </manifest> 

Maybe a typo when you pasted the code here, but still.

+1
source

Source: https://habr.com/ru/post/1379766/


All Articles