Create an application to read file from asset folder and copy it in memory card.
MainActivity.java
MainActivity.java
package com.example.assignment16;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class MainActivity extends AppCompatActivity {
Button btnRead, btnMove;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnRead = (Button) findViewById(R.id.button1);
btnRead.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
BufferedReader myReader = new BufferedReader(new InputStreamReader(getAssets().open("mytext.txt")));
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 View.OnClickListener() {
@Override
public void onClick(View v) {
try {
String destPath = "/sdcard/mytext.txt";
File f = new File(destPath);
if (!f.exists()) {
CopyAsset(getBaseContext().getAssets().open("mytext.txt"), new FileOutputStream(destPath));
}
Toast.makeText(getBaseContext(),"Moved successfully", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), " " + e.toString(), Toast.LENGTH_LONG).show();
}
}
});
}
private void CopyAsset(InputStream open, FileOutputStream fileOutputStream) throws IOException {
{
//---copy 1K bytes at a time---
byte[] buffer = new byte[1024];
int length;
if ((length = open.read(buffer)) > 0) //check the length of file
{
fileOutputStream.write(buffer, 0, length);
}
open.close();
fileOutputStream.close();
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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"
tools:context=".MainActivity">
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="-3dp"
android:layout_y="241dp"
android:text="Read File" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="-4dp"
android:layout_y="285dp"
android:text="Move File To SD Card" />
</AbsoluteLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.assignment16">
<uses-sdk android:minSdkVersion="8"></uses-sdk>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
</uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
mytext.txt
Hello World!!
No comments:
Post a Comment