GTU Android Program 16 Create an application to read file from asset folder and copy it in memory card (external device)

      Comments Off on GTU Android Program 16 Create an application to read file from asset folder and copy it in memory card (external device)

GTU Android Program 16 Create an application to read file from asset folder and copy it in memory card (external device)

GTU Android Program 16

Android developer stores some useful files into assets folder of android project directory if they want to expert this file to sdcard then they can refer the following code which shows how to read file from assets folder and write into memory card (external device)

Step 1 : Create android project.

Step 2 : create readme.txt file save it to desktop, then copy paste this file into assets folder (alternatively you can paste into project/assets folder of workspace where you have stored your project)

Step 3 : Add following permission into AndriodManifest.xml file.

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

 Step 4 : write down code as explain below

Step 5 : Run application for testing in Android Virtual Device (AVD) Make sure to add sdcard size to some MB(eg. 10), otherwise sdcard option will not be attached to your Android Virtual Device (AVD)

If you are testing using your attached mobile then SD card is required in mobile to test this application.

Step 6 : to check whether file has been copied into sdcard or not, open DDMS, Select working AVD, goto File Explorer, goto mnt > sdcard (your file will be here…)

Location : res\layout

FileName: 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:background="#fce4ec"
    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="gtu.android.prog16.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#e91e63"
        android:text="Read File from assets and Write into sdcard (external storage)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="52dp"
        android:textColor="#9e9e9e"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text=""
        android:textColor="#d81b60"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

Location : src\package\

FileName : MainActivity.java

package gtu.android.prog16;

/*
 *  GTU Android Program 16 Create an application to read file from asset folder and copy it in memory card (external device)
 * 
 */
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

	TextView tv3;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
	
		tv3 = (TextView) findViewById(R.id.textView3);
		
		try 
		{

			InputStream is = getResources().getAssets().open("readme.txt");

			//===============================================
			// Read data of readme.txt in byte format and store into b array
			//==============================================
			byte[] b = new byte[is.available()];


			String locationToStoreFile = Environment.getExternalStorageDirectory().getPath() + "/readme.txt";
			//Toast.makeText(getApplicationContext(), , Toast.LENGTH_SHORT).show();
			
			tv3.setText("File Stored at : " + locationToStoreFile);
			
			FileOutputStream fos = new FileOutputStream(new File(locationToStoreFile));

			//==========================================
			// Write byte array data into file output stream object, location is external storage
			//==========================================
			fos.write(b);
			fos.close();

		} catch (IOException e) 
		{
			e.printStackTrace();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Output

GTU Android Program 16