This sample android program shows you how read a file in Android. In this program an embedded file which is saved in your project under the res/assets folder .the txt file read in TextView .....
first time design main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/white" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/tv1"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
<TextView
android:id="@+id/tv2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="15dp"
android:textColor="@color/black"
/>
</LinearLayout>
</ScrollView>
second MainActivity.java-
package com.Androidbeginnerpint.example;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
public class About_Us extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.about_us);
TextView txtContent=(TextView)findViewById(R.id.tv1);
TextView txtFileName=(TextView)findViewById(R.id.tv2);
AssetManager assetManager=getAssets();
try {
String[] files=assetManager.list("Files");
for (int i = 0; i < files.length; i++) {
txtContent.append("\n Files=>"+i+"Name"+files);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
InputStream input;
try {
input=assetManager.open("about.txt");
int size=input.available();
byte[] buffer=new byte[size];
input.read(buffer);
input.close();
String text=new String(buffer);
txtFileName.setText(text);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
overridePendingTransition(R.anim.stay, R.anim.slide_out_right);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
0 comments