ReadAssetsFileActivity.java
package com.readassets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ReadAssetsFileActivity extends Activity {
/** Called when the activity is first created. */
Button btnRead,btnMove;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnRead=(Button)findViewById(R.id.button1);
btnRead.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
BufferedReader myReader = new BufferedReader(new
InputStreamReader(getAssets().open("mytxt")));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
Toast.makeText(getBaseContext(),"Data : "+aBuffer,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(),
e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
btnMove=(Button)findViewById(R.id.button2);
btnMove.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
String destPath = "/sdcard/mytxt";
File f = new File(destPath);
if (!f.exists())
{
CopyDB(
getBaseContext().getAssets().open("mytxt"),new FileOutputStream(destPath));
}
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), " "+e.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
public void CopyDB(InputStream inputStream,OutputStream
outputStream)throws IOException
{
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) //check the
length of file
{
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Read File" android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="match_parent"></Button>
<Button android:text="Move File To SD Card" android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="match_parent"></Button>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.readassets"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".ReadAssetsFileActivity"
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>
Output
package com.readassets;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ReadAssetsFileActivity extends Activity {
/** Called when the activity is first created. */
Button btnRead,btnMove;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnRead=(Button)findViewById(R.id.button1);
btnRead.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
BufferedReader myReader = new BufferedReader(new
InputStreamReader(getAssets().open("mytxt")));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";
}
myReader.close();
Toast.makeText(getBaseContext(),"Data : "+aBuffer,
Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(),
e.getMessage(),Toast.LENGTH_SHORT).show();
}
}
});
btnMove=(Button)findViewById(R.id.button2);
btnMove.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try
{
String destPath = "/sdcard/mytxt";
File f = new File(destPath);
if (!f.exists())
{
CopyDB(
getBaseContext().getAssets().open("mytxt"),new FileOutputStream(destPath));
}
}
catch (Exception e)
{
Toast.makeText(getBaseContext(), " "+e.toString(),
Toast.LENGTH_LONG).show();
}
}
});
}
public void CopyDB(InputStream inputStream,OutputStream
outputStream)throws IOException
{
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) //check the
length of file
{
outputStream.write(buffer, 0, length);
}
inputStream.close();
outputStream.close();
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:text="Read File" android:id="@+id/button1"
android:layout_height="wrap_content"
android:layout_width="match_parent"></Button>
<Button android:text="Move File To SD Card" android:id="@+id/button2"
android:layout_height="wrap_content"
android:layout_width="match_parent"></Button>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.readassets"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<application android:icon="@drawable/icon"
android:label="@string/app_name">
<activity android:name=".ReadAssetsFileActivity"
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>
Output
To download follow this Link : ReadAssetsFile.zip
No comments:
Post a Comment