Skip to main content

How to Import file from external storage - Android Tutorial

In this example, we can see how to import the file in external location. Fetching the data from that file and show it in Log-cat as well as in List-view. 




This simple example, contains one button (Click to import from SD card) to access the file data from an external location.

Now follow the coding to develop this simple application  

Create a New Project

1. activity_main.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"   
android:layout_width="match_parent"    
android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.example.readfile.MainActivity" >
<Button        
android:id="@+id/btnclick"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:text="@string/click" />    
<View        
ndroid:id="@+id/viewid"       
 android:layout_width="match_parent"        
android:layout_height="10dp"       
 android:layout_below="@+id/btnclick"        
android:background="#7FFF00" />    
<ListView        
android:id="@+id/name_list"        
android:layout_width="wrap_content"        
android:layout_height="wrap_content"        
android:layout_below="@+id/viewid" >    
</ListView>
</RelativeLayout>

2. MainActivity.java


public class MainActivity extends Activity {

Button click;
ArrayList<String> array;
ListView lv;

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

click = (Button) findViewById(R.id.btnclick);
lv = (ListView) findViewById(R.id.name_list);

click.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
String state = Environment.getExternalStorageState();
if (!(state.equals(Environment.MEDIA_MOUNTED))) {
Toast.makeText(null, "There is no sd card",
Toast.LENGTH_LONG).show();
} else {
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard, "/Names/Names");
BufferedReader br = new BufferedReader(new FileReader(
file));
                                               String st = "";
array = new ArrayList<String>();
while ((st = br.readLine()) != null) {
Log.e("", "" + st);
array.add(st);
Log.e("ARRAY", "" + array);
}
setlist(array);
}

} catch (Exception e) {
Log.e("", "" + e);
}
}

});
}

private void setlist(ArrayList<String> array2) {
// TODO Auto-generated method stub

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, array2);
lv.setAdapter(arrayAdapter);

}
}

3. Manifest

Finally give permission to access from external storage

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


When touch/click the button, the values from the file in the given path in external storage will get read. 

Below image illustrates the file in an external storage from which we have to get values.


Output 



__________________________________________________________________

Source code for this application ImportFilefromExternalStorage.zip

                                                Happy Coding...

Comments

Popular posts from this blog

Zoom Image - Android Tutorial

Here we are going to see how to zoom an image in Imageview Will see it through a sample 1. Create xml with an ImageView <? xml version="1.0" encoding="utf-8" ?> < androidx.constraintlayout.widget.ConstraintLayout       xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:app="http://schemas.android.com/apk/res-auto"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     tools:context=".MainActivity" >     < LinearLayout         android:layout_width="200dp"         android:layout_height="200dp"         app:layout_constraintBottom_toBottomOf="parent"         app:layout_constraintLeft_toLeftOf="parent"         app:layout_constraintRight_toRightOf="parent"    ...

Add custom font in Android using Calligraphy library

Are you fed up of Custom Views to set fonts? Or traversing the ViewTree to find TextViews? Sometime we want some other font for our Android application then you can add custom font in Android using Calligraphy library . Dependency Include the dependency Download (.aar) dependencies { compile ‘uk.co.chrisjenx:calligraphy:2.2.0’ } Add Fonts Add your custom fonts to assets/ . All font definitions are relative to this path. On Assets you should right-click New Directory, call it "fonts". In the finder put the .ttf  or .otf  font files in there. Create Class Create a class that extends Application and write this code public class App extends Application { @Override public void onCreate() { super.onCreate(); CalligraphyConfig.initDefault(new CalligraphyConfig.Builder() .setDefaultFontPath("your font path") .setFontAttrId(...

Bluetooth Chat Application - Android Tutorial

In this tutorial, we will see about how to design an Android layout for chat application using Chat Bubbles  and the main part is chat via Bluetooth . Main objective of this post is to give an idea about how to allow two-way text chat over Bluetooth in android. Bubbles: Chat bubbles are background image that expands horizontally and vertically as required based on the message posted. Bubbles are Nine-patch Images. Image Nine-patch Image In creating Android chat bubbles, nine-patch image plays a crucial role.  Nine-patch image  is a bitmap which stretches to fit the content posted in the View where it is applied as a background. A NinePatch drawable is a standard PNG image that includes an extra 1-pixel-wide border. It must be saved with the extension  .9.png , and saved into the  res/drawable/  directory of your project. The border is used to define the stretchable and static areas of the image. You indicate a stretchable section ...