0% found this document useful (0 votes)
16 views6 pages

Assignment 8 MC 007

Ass

Uploaded by

sujata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views6 pages

Assignment 8 MC 007

Ass

Uploaded by

sujata
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

SIES College of Management Studies SYMCA (Revised), Sem III,

Roll No: 07

Assignment - 8

Write an android program for Internal storage and external storage (Input the file name
(edit text) and contents (multiline edit text), 4 buttons), read and show the contents in the
next activity textview.
Code:
MainActivity.java:
package com.example.p8;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class MainActivity extends AppCompatActivity {


EditText ed_fn,ed_cont;
Button btn_save_to_internal, btn_save_to_external, btn_ret_from_internal,
btn_ret_from_external;
String str;
File myFile;
String filepath = "myDir";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed_fn = findViewById(R.id.ed_fn);
ed_cont = findViewById(R.id.ed_cont);
btn_save_to_internal = findViewById(R.id.btn_save_to_internal);
btn_save_to_external = findViewById(R.id.btn_save_to_external);
btn_ret_from_internal = findViewById(R.id.btn_ret_from_internal);
btn_ret_from_external = findViewById(R.id.btn_ret_from_external);

//Save to Internal
btn_save_to_internal.setOnClickListener(new View.OnClickListener() {
@Override
Subject: MCAL34 Mobile Computing Lab Academic Year
First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

public void onClick(View view) {


String fn = ed_fn.getText().toString();
str = ed_cont.getText().toString();
saveToInternal(fn);
}
});

//Retrieve from Internal


btn_ret_from_internal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fn = ed_fn.getText().toString();
Intent next_act = new Intent(getApplicationContext(),StringAct.class);
next_act.putExtra("Type","Internal");
next_act.putExtra("FileName",fn);
startActivity(next_act);
}
});

//Save to External
btn_save_to_external.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fn = ed_fn.getText().toString();
str = ed_cont.getText().toString();
saveToExternal(fn);
}
});

//Retrieve from External


btn_ret_from_external.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String fn = ed_fn.getText().toString();
Intent next_act = new Intent(getApplicationContext(),StringAct.class);
next_act.putExtra("Type","External");
next_act.putExtra("FileName",fn);
startActivity(next_act);
}
});
}
private void saveToInternal(String fn) {
try {
FileOutputStream fos = openFileOutput(fn,MODE_PRIVATE);
OutputStreamWriter writer = new OutputStreamWriter(fos);
writer.write(str);
writer.close();
Subject: MCAL34 Mobile Computing Lab Academic Year
First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),"Successfully saved to Internal
Storage",Toast.LENGTH_LONG).show();
}

private void saveToExternal(String fn) {


String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState) & !
Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
myFile = new File(getExternalFilesDir(filepath), fn);
}
try {
FileOutputStream fos = new FileOutputStream(myFile);
fos.write(str.getBytes());
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),"Successfully saved to External
Storage",Toast.LENGTH_LONG).show();
}
}

StringAct.java:
package com.example.p8;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class StringAct extends AppCompatActivity {


TextView tv1;
String fn,f_type;
File myFile;
String filepath = "myDir";
@Override
Subject: MCAL34 Mobile Computing Lab Academic Year
First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.activity_string);
tv1= findViewById(R.id.tv1);
fn = getIntent().getStringExtra("FileName");
f_type = getIntent().getStringExtra("Type");
if (f_type.equals("Internal"))
{
readFromInternal(fn);
} else if (f_type.equals("External")) {
readFromExternal(fn);
}
else {
tv1.setText("Something went wrong.");
}
}
//Internal
private void readFromInternal(String fn) {
String content = "";
try {
FileInputStream fis = openFileInput(fn);
InputStreamReader reader = new InputStreamReader(fis);
char[] buffer = new char[256];
int charRead;
while ((charRead=reader.read(buffer))>0){
String rs = String.copyValueOf(buffer,0,charRead);
content += rs;
}

} catch (Exception e) {
e.printStackTrace();
}
tv1.setText(content);
}
//External
private void readFromExternal(String fn) {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState) & !
Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
myFile = new File(getExternalFilesDir(filepath), fn);
}
Subject: MCAL34 Mobile Computing Lab Academic Year
First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

String content = "";


try {
FileInputStream fis = new FileInputStream(myFile);
DataInputStream dis = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(dis));
String line;
while((line=br.readLine())!=null){
content = content + "\n" + line;
}
dis.close();
} catch (Exception e) {
e.printStackTrace();
}
tv1.setText(content);
}
}

Output:

External Storage:

Internal Storage:

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24
SIES College of Management Studies SYMCA (Revised), Sem III,
Roll No: 07

Subject: MCAL34 Mobile Computing Lab Academic Year


First Half 2023_24

You might also like