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();
}
}
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();
}
}
}
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>
<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>
Labels:
Android,
Mobile Computing
Subscribe to:
Post Comments (Atom)
Subjects
- WordPress
- Mobile Computing-4649303 Practical Solution
- Android Programming New Syllabus Theory
- PHP LAMP Question Bank
- PHP LAMP Theory
- Step by Step Android Example
- Android Practical
- Android Theory
- Android Question Bank
- Networking FON Practical
- Networking FON Theory
- OS Practical
- OS Theory
- HTML
- JavaScript
- J2EE WTAD Theory
- J2EE WTAD Question Bank
- J2EE WTAD Quick Guide
- J2EE WTAD GTU Papers
- J2EE WTAD Practical
- Python
- JAVA Theory
- JAVA Practical
- MIS
Categories
- Android (55)
- CSS (3)
- Configure Tomcat7 (2)
- Decryption (16)
- Difference (1)
- Encryption (16)
- Error Detection and Correction Techniques (3)
- FON (27)
- Framing Technic (2)
- J2EE (29)
- JAVA (13)
- JavaScript (19)
- OS (17)
- PHP (11)
- Protocol (3)
- SERVER SOCKET PROGRAMING (7)
- Servlet (13)
- WTAD (34)
- c (11)
- install Tomcat (2)
- linux (8)
- shell script (33)
- unix (22)
Total Pageviews
© BipinRupadiya.com. Powered by Blogger.
0 comments:
Post a Comment