ViewInformationActivity.java
unknown
java
3 years ago
2.7 kB
8
Indexable
package com.hgo.externalstorage;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class ViewInformationActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_information);
// findViewById returns a view, we need to cast it into TextView
textView = (TextView) findViewById(R.id.textView_get_saved_data);
}
public void showPublicData(View view) {
// Accessing the saved data from the downloads folder
File folder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
// geeksData represent the file data that is saved publicly
File file = new File(folder, "myfiledata.txt");
String data = getdata(file);
if (data != null) {
textView.setText(data);
} else {
textView.setText("No Data Found");
}
}
public void showPrivateData(View view) {
// GeeksForGeeks represent the folder name to access privately saved data
File folder = getExternalFilesDir("mydir");
// gft.txt is the file that is saved privately
File file = new File(folder, "myfile.txt");
String data = getdata(file);
if (data != null) {
textView.setText(data);
} else {
textView.setText("No Data Found");
}
}
public void back(View view) {
Intent intent = new Intent(ViewInformationActivity.this, MainActivity.class);
startActivity(intent);
}
// getdata() is the method which reads the data
// the data that is saved in byte format in the file
private String getdata(File myfile) {
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(myfile);
int i = -1;
StringBuffer buffer = new StringBuffer();
while ((i = fileInputStream.read()) != -1) {
buffer.append((char) i);
}
return buffer.toString();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
}
Editor is loading...