postheadericon copy file from Assets to SDcard in Android Example



Definition : 16. Create an application to read file from asset folder and copy it in memory card.


File Name : E16.java


package bsr.exa;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;



import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class E16 extends Activity
{
/**  @author Bipin S Rupadiya , www.bipinrupadiya.com
     *
     *  16. Create an application to read file from asset folder and copy it in memory card.
     *
     *  */

FileOutputStream fos ;
 
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.s1);        
   
        try
{
String destPath = "/sdcard/bsr.txt";
File f = new File(destPath);
if (!f.exists())
{
CopyDB( getBaseContext().getAssets().open("file.txt"),new FileOutputStream(destPath));
}
dispFile();
}
catch (Exception e)
{
Toast.makeText(E16.this, " "+e.toString(), Toast.LENGTH_LONG).show();
}
}
    public void dispFile()
    {
    try
{
File fileDir = Environment.getExternalStorageDirectory();
        File directory = new File(fileDir.getAbsolutePath());
        File file = new File(directory , "bsr.txt");
        FileInputStream fis = new FileInputStream(file);
String str = null;
StringBuffer sbuffer = new StringBuffer();
DataInputStream dataio = new DataInputStream(fis);
while((str = dataio.readLine()) != null)
{
sbuffer.append(str + "\n");
}
TextView txt=(TextView)findViewById(R.id.showTxt);
txt.setText(sbuffer);
}
catch (Exception e)
{
Toast.makeText(this, " "+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();
    }
}

File Name : screen1.java


package bsr.exa;

import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import android.widget.Toast;

public class screen1 extends Activity
{
/**  @author Bipin S Rupadiya , www.bipinrupadiya.com
      */

public void onCreate(Bundle b)
{
super.onCreate(b);
setContentView(R.layout.s1);
try
{
File fileDir = Environment.getExternalStorageDirectory();
        File directory = new File(fileDir.getAbsolutePath());
        File file = new File(directory , "bsr.txt");
        FileInputStream fis = new FileInputStream(file);
String str = null;
StringBuffer sbuffer = new StringBuffer();
DataInputStream dataio = new DataInputStream(fis);
while((str = dataio.readLine()) != null)
{
sbuffer.append(str + "\n");
}
TextView txt=(TextView)findViewById(R.id.showTxt);
txt.setText(sbuffer);
}
catch (Exception e)
{
Toast.makeText(this, " "+e.toString(), Toast.LENGTH_LONG).show();
}
}
}


File Name : AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      android:versionCode="1"
      android:versionName="1.0" package="bsr.exa">
    <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:label="@string/app_name" android:name=".E16">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>
</manifest>





0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.