public void getFile(){ //Get the text file File file = new File(newFolder,"MyTest.txt"); //Read text from file StringBuilder text = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { text.append(line); text.append('\n'); } br.close(); } catch (IOException e) { Log.i("TAG","ERROR." + e); } //Find the view by its id TextView tv = (TextView)findViewById(R.id.showFile); //Set the text tv.setText(text.toString()); }
Monthly Archives: May 2017
Android. Create folder on SD card and write data
This will create a folder called TestFolder in /storage/emulated/0/ and write
to a text file called MyTest.txt in that folder.
File newFolder;//path to folder File file;//path to file public void writeData() { createFolderIfNoneExists(); FileWriter writer = null; try { writer = new FileWriter(file); // Writes the content to the file writer.write("This\n is\n an\n example\n"); writer.flush(); writer.close(); } catch (IOException e) { e.printStackTrace(); } } public void createFolderIfNoneExists(){ try { newFolder = new File(Environment.getExternalStorageDirectory(), "TestFolder"); if (!newFolder.exists()) { newFolder.mkdir(); } try { file = new File(newFolder, "MyTest" + ".txt"); file.createNewFile(); } catch (Exception ex) { Log.i("TAG", "Exception: " + ex); } } catch (Exception e) { Log.i("TAG", "Exception 2: " + e); } }
To view the contents of the file you will need a reference to the folders path (The ‘folder’ variable in the above code) and the name of the file.
then use this code:
Retrieve file from SD card
Check if you can Read or write to SD Card android
The following method will check and see what read and write options are available
before writing to the SD card.
public void writeTocard(){ Log.i("TAG", "writeTocard started"); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media Log.i("TAG", "We can read and write to card"); mExternalStorageAvailable = mExternalStorageWriteable = true; } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media Log.i("TAG", "We can only read to card"); mExternalStorageAvailable = true; mExternalStorageWriteable = false; } else { // Something else is wrong. It may be one of many other states, but all we need // to know is we can neither read nor write Log.i("TAG", "We can NOT read or write to card"); mExternalStorageAvailable = mExternalStorageWriteable = false; } }
Install Stetho to view SQLite and local files on android device.
Create java file MyApplication
import android.app.Application; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import com.facebook.stetho.Stetho; public class MyApplication extends Application { public void onCreate() { //Log.i("TAG","MyApplication started"); super.onCreate(); //Stetho.initializeWithDefaults(this); // Create an InitializerBuilder Stetho.InitializerBuilder initializerBuilder = Stetho.newInitializerBuilder(this); // Enable Chrome DevTools initializerBuilder.enableWebKitInspector( Stetho.defaultInspectorModulesProvider(this) ); // Enable command line interface initializerBuilder.enableDumpapp( Stetho.defaultDumperPluginsProvider(this) ); // Use the InitializerBuilder to generate an Initializer Stetho.Initializer initializer = initializerBuilder.build(); // Initialize Stetho with the Initializer Stetho.initialize(initializer); } }
In the androidManifest file add:
<application android:name=".MyApplication">
In the build.gradle(Module.app) add the following to the dependencies:
//Stetho is used to see SQLite database in Chrome. Go to Chrome://inspect // Stetho core compile 'com.facebook.stetho:stetho:1.3.1' //Optional network helper compile 'com.facebook.stetho:stetho-okhttp:1.3.1' compile 'com.facebook.stetho:stetho-js-rhino:1.4.2'