I'm new to Android development and I want to replace the file in TextView with text from file on external storage and show it in textView1. I searched many questions, but the solutions didn't work for me. Thanks for help. Here is my code:
Text View in fragment_profile:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="@string/nick"
android:textColor="#FFFFFF" />
And this is the activity:
package com.tom411.pyzdit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ProfileActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
TextView tv = (TextView) findViewById(R.id.textView1);
File dir = Environment.getExternalStorageDirectory();
File file = new File(dir,"/PyzdIt/name.txt");
if(file.exists())
{
StringBuilder text = new StringBuilder();
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.read();
br.close();
}
catch (IOException e) {
}
tv.setText(text);
}
}
public void Back(View view)
{
Intent intent = new Intent(ProfileActivity.this, MainActivity.class);
startActivity(intent);
}
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile,
container, false);
return rootView;
}
}
}
I haven't tested your code yet but the first thing that I would check for is the access to ExternalStorage . Maybe you have got to put a <uses-permission>
entry in app manifest to allow access to the external storage (android.permission.READ_EXTERNAL_STORAGE).
use this code its works fine :
File logFile = new File("sdcard/text.txt");
if (logFile.exists())
{
try
{ FileInputStream fIn = new FileInputStream(logFile);
BufferedReader myReader = new BufferedReader(
new InputStreamReader(fIn));
String aDataRow = "";
String aBuffer = "";
while ((aDataRow = myReader.readLine()) != null) {
aBuffer += aDataRow + "\n";}
text.setText(aBuffer);
myReader.close();
logFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
and be sure use the permission on manifest
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.