Code of a simple android app which can browse and open a text file

Here I provide the codes of a simple android app which can be used to browse and open a text file from device.

Codes in main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Button"
android:id="@+id/select"/>
<EditText
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:ems="10"
android:id="@+id/edittext1"/>
<EditText
android:layout_height="match_parent"
android:layout_width="match_parent"
android:ems="10"
android:id="@+id/edittext2"/>
</LinearLayout>

Codes in MainActivity.java
package com.myfile.open;

import android.app.*;
import android.os.*;
import android.widget.*;
import android.view.*;
import android.view.View;
import android.content.*;
import java.io.*;

public class MainActivity extends Activity 
{
private Button btn;
private EditText edittext1;
private EditText edittext2;
private static final int PICKFILE_RESULT_CODE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

btn = (Button) findViewById(R.id.select);
edittext1 = (EditText) findViewById(R.id.edittext1);
edittext2 = (EditText) findViewById(R.id.edittext2);

btn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("text/*");
startActivityForResult(intent,PICKFILE_RESULT_CODE);}
});
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == RESULT_OK) {
switch(requestCode) {
case PICKFILE_RESULT_CODE:

String selectedFile = data.getData().getPath();
String filename= selectedFile.substring(selectedFile.lastIndexOf("/")+1);
edittext1.setText(filename);
selectedFile = selectedFile.substring(selectedFile.lastIndexOf(":")+1);

try {
File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + selectedFile);
FileInputStream fIn = new FileInputStream( myFile );
BufferedReader myReader = new BufferedReader( new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) { aBuffer+= aDataRow + "\n";}
edittext2.setText(aBuffer);
myReader.close();
Toast.makeText(getBaseContext(),"Done reading file "+filename+" from sdcard",Toast.LENGTH_SHORT).show();}
catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),Toast.LENGTH_SHORT).show();}

break;
         }
      }
   }

}

Codes in AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
package= "com.myfile.open" >
<uses-permission android:name= "android.permission.WRITE_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>

Note that this app requires permissions to WRITE EXTERNAL STORAGE.

The code has been described in this video:

Comments

  1. Buena como hago eso a la inversa. Como copio un archivo de texto a sketcware

    ReplyDelete
    Replies
    1. In Sketchware it cannot be done only because it needs permissions to WRITE EXTERNAL STORAGE in AndroidManifest.xml.

      If Sketchware,
      Code in onCreate:

      } @Override
      protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      if(resultCode == RESULT_OK) {
      switch(requestCode) {
      case PICKFILE_RESULT_CODE:
      String selectedFile = data.getData().getPath();
      String filename= selectedFile.substring(selectedFile.lastIndexOf("/")+1);
      edittext1.setText(filename);
      selectedFile = selectedFile.substring(selectedFile.lastIndexOf(":")+1);
      try {
      File myFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + selectedFile);
      FileInputStream fIn = new FileInputStream(myFile);
      BufferedReader myReader = new BufferedReader( new InputStreamReader(fIn));
      String aDataRow = "";
      String aBuffer = "";
      while ((aDataRow = myReader.readLine()) != null) { aBuffer += aDataRow + "\n"; }
      edittext2.setText(aBuffer);
      myReader.close();
      Toast.makeText(getBaseContext(), "Done reading file "+filename+" from sdcard", Toast.LENGTH_SHORT).show(); }
      catch (Exception e) {
      Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); }
      break; } }


      Code in on button click:

      Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
      intent.setType("text/*");
      startActivityForResult(intent,PICKFILE_RESULT_CODE);

      Then export and add permissions in manifest.

      Delete
  2. How can i access mp3 files in android, store it in firebase and, through there use those songs in my audio player app

    ReplyDelete
  3. How can i access mp3 files in android, store it in firebase and, through there use those songs in my audio player app

    ReplyDelete
    Replies
    1. You get to store only 5 GB in Firebase storage. And storage cannot be used in Sketchware yet. Anyway​ Loading songs from firebase will consume lots of data, and will be very slow.

      Delete
    2. i think we need apk editor for that

      Delete
    3. How can i access all files with extension .png, .jpg, .mp3, .mp4 and do operations on them

      Delete
  4. How can save files to external storage

    ReplyDelete

Post a Comment

Popular posts from this blog

A Flash Light App in Sketchware

Create Music Player app in Sketchware

Simple car racing android game in Sketchware

In Sketchware: How to create a calculator for any simple formula?

Firebase auth in Sketchware for Login Activity