Android IDE (AIDE) Support Forum

Create and write to a text file on SD card

Hi
How do I write to SD card
I have checked card is mounted
I have given permission in android manifest
Must be missing something?

1 Like

@Longjohn Depending on your Android version, SD cards are not supported. Tried to find out more (from devs), but they haven’t replied yet. Sorry.

Thanks
I have a text file that contains thousands of words and I want to sort it into separate files, depending on the number of letters in the word.
I am going to try working with external storage instead of sd card.
Thanks Longjohn.

@LongJhon send source code sir, i want to lesson

Hi maskaywalanay
Just seen your question
Will look it up and get back to you in the next couple of days
longjohn

I have found the code which you can examine
My tablet tells me it contains a virus but this is impossible
Should be able to adapt to what you want.
This code allows you to enter text which can then be saved to the SD card and the file path is shown.

Java

package com.mycompany.third_try;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.app.Activity;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText inputText;
TextView response;
Button saveButton,readButton;
private String filename = “SampleFile.txt”;
private String filepath = “MyFileStorage”;
File myExternalFile; String myData = “”;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
inputText = (EditText) findViewById(R.id.myInputText);
response = (TextView) findViewById(R.id.response);
saveButton = (Button) findViewById(R.id.saveExternalStorage);
saveButton.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
try {
FileOutputStream fos = new FileOutputStream(myExternalFile);
fos.write(inputText.getText().toString().getBytes());
fos.close();
}
catch (IOException e) {
e.printStackTrace();
}
inputText.setText("");
response.setText(“SampleFile.txt saved to External Storage…”+myExternalFile); }
});
readButton = (Button) findViewById(R.id.getExternalStorage);
readButton.setOnClickListener(new OnClickListener() {
@Override public void onClick(View v) {
try {
FileInputStream fis = new FileInputStream(myExternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine; }
in.close(); }
catch (IOException e) {
e.printStackTrace(); }
inputText.setText(myData);
response.setText(“SampleFile.txt data retrieved from External Storage…”);
} });
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
saveButton.setEnabled(false); }
else {
myExternalFile = new File(getExternalFilesDir(filepath), filename);
} }
private static boolean isExternalStorageReadOnly() { String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
private static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
}

2 Likes

Main xml

<
LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:orientation=“vertical”>
<
TextView android:layout_width=“fill_parent”
android:layout_height=“wrap_content”
android:text=“Reading and Writing to External Storage”
android:textSize=“24sp”/>
<
EditText android:id="@+id/myInputText"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:ems=“10” android:lines=“5”
android:minLines=“3”
android:gravity=“top|left”
android:inputType=“textMultiLine”>
<
requestFocus />
<
/EditText>
<
LinearLayout
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:orientation=“horizontal”
android:weightSum=“1.0”
android:layout_marginTop=“20dp”>
<
Button
android:id="@+id/saveExternalStorage"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“SAVE”
android:layout_weight=“0.5”/>
<
Button
android:id="@+id/getExternalStorage"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_weight=“0.5”
android:text=“READ” />
<
/LinearLayout>
<
TextView
android:id="@+id/response"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:padding=“5dp”
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
<
/LinearLayout>

2 Likes

Android manifest

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

<
manifest xmlns:android=“http://schemas.android.com/apk/res/android”
package=“com.mycompany.third_try” >
<
uses-permission android:name=“android.permission.WRITE_EXTERNAL_STORAGE”/>
<
uses-permission android:name=“android.permission.READ_EXTERNAL_STORAGE”/>
<
application
android:allowBackup=“true”
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
android:resizeableActivity = “true”>
<
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>

<
/manifest>

2 Likes

well thank you sir, you are very kind to give me the source code

error di bagian ini pak, apa ada variabel atau kata lain mungkin

I don’t understand your language
But what you are showing me is different to the code I posted
Longjohn

I really appreciate for the source code.

Thank you so much for the source code.

Hi there. I actually spent a long long time on this trying to get my app to write “anywhere” on an SD card like you would do with a desktop computer.
Most (if not all) modern versions of Android actually restrict where you can write to. Every App installed in Android has it’s own folder (the same name as the app)- usually this folder exists on both the internal storage of the device AND the SD card. You can write freely to these folders because the app itself “owns” them. However, other apps can’t necessarily READ from these folders. Only your app.

You can also write to particular “public” folders on the SD card such as “pictures” or “documents” (the exact name depends on your device). All apps can share these particular folders and use them to exchange files between the apps if desired, for example.

But what you cannot do is just write ANYWHERE. For instance you can’t write to the root folder of the SD card, nor can you make any new folder you like on the SD card and write to it. Only the App’s own folder on the SD card and those “public” folders can be written to. Once you’ve accepted this (unlike me who struggled to find a work-around because I’m stubborn) it’s actually easy and works well.

You can, however, READ from anywhere on the SD card (provided you have got permission from the user to do this).

You might be wondering then: How does my phone/tablet’s built in file-manager write ANYWHERE on the SD card, which it seems to do with no problems?

Well the answer is the manufacturers of the device have special access and can enable their own file-manager app to write on the SD card easily. But for the normal Android user (that means you and me and anyone using AIDE etc) you can’t do that. It needs low-level Android access which Android restricts only to manufacturers or people who have rooted/jailbroken their phones.

Hope this helps :slight_smile:

1 Like