应用程序启动android时如何从内部存储中读取文件
Posted
技术标签:
【中文标题】应用程序启动android时如何从内部存储中读取文件【英文标题】:How to read file from internal storage when app start android 【发布时间】:2017-09-15 07:38:25 【问题描述】:我得到了保存更改的按钮,并将其放入内部存储中,然后当应用程序启动时,如果有任何 ofc,我想读取文件。我认为这就足够了,但我是新鲜的编码器,但我不知道这很好。
我不知道哪里出错了,那也是很好的代码,请帮助我,因为我卡住了
我的代码:
公共类 MyGameDetailsFragment 扩展片段
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_my_game_details, container, false);
try
FileInputStream is = getActivity().openFileInput(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");
is.close();
catch(OutOfMemoryError om)
om.printStackTrace();
catch(Exception ex)
ex.printStackTrace();
String result = sb.toString();
saveBtn = (Button) view.findViewById(R.id.savechangesbtn);
saveBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Data1 = s1.getText().toString();
Data2 = s2.getText().toString();
Data3 = s3.getText().toString();
Data4 = s4.getText().toString();
Data5 = s5.getText().toString();
Data6 = s6.getText().toString();
try
FileOutputStream fos=getActivity().openFileOutput(file, getActivity().MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(fos);
outputStream.write(Data1.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data2.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data3.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data4.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data5.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data6.getBytes());
outputStream.close();
//Toast.makeText(context,"file saved",Toast.LENGTH_SHORT).show();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
try
FileInputStream inputStream = getActivity().openFileInput(file);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
total.append(line);
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
catch (Exception e)
e.printStackTrace();
);
return view;
Ps:s1,s2等是在代码等中创建的
权限
<uses-permission android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
【问题讨论】:
【参考方案1】:public void onViewCreated(View view, Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
Other = (TextView) view.findViewById(R.id.Details);
OtherD = (TextView) view.findViewById(R.id.OtherDetails);
try
FileInputStream is = getActivity().openFileInput(file);
byte[] input = new byte[is.available()];
while(is.read(input) != -1)
value += new String(input);
is.getFD().sync();
//is.flush();
is.close();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
String[] strArray = value.split(";");
s1.setText(strArray[1]);
s2.setText(strArray[2]);
s3.setText(strArray[3]);
s4.setText(strArray[4]);
s5.setText(strArray[5]);
s6.setText(strArray[6]);
s7.setText(strArray[7]);
saveBtn = (Button) view.findViewById(R.id.savechangesbtn);
saveBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Data1 = s1.getText().toString();
Data2 = s2.getText().toString();
Data3 = s3.getText().toString();
Data4 = s4.getText().toString();
Data5 = s5.getText().toString();
Data6 = s6.getText().toString();
Data7= s7.getText().toString();
try
FileOutputStream fos=getActivity().openFileOutput(file, getActivity().MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(fos);
outputStream.write(string3.getBytes());
outputStream.write(Data1.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data2.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data3.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data4.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data5.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data6.getBytes());
outputStream.write(string3.getBytes());
outputStream.write(Data7.getBytes());
outputStream.write(string3.getBytes());
//outputStream.getFD().sync();
outputStream.flush();
outputStream.close();
//Toast.makeText(context,"file saved",Toast.LENGTH_SHORT).show();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
try
FileInputStream inputStream = getActivity().openFileInput(file);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
total.append(line);
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
catch (Exception e)
e.printStackTrace();
);
自己解决
【讨论】:
【参考方案2】:首先我建议你,不要在片段中在 OnCreateView() 方法中编写代码,始终在 OnViewCreated 方法中编写代码。
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
View view = inflater.inflate(R.layout.fragment_my_game_details, container, false);
return view;
public void onViewCreated(View view, Bundle savedInstanceState)
super.onViewCreated(view, savedInstanceState);
try
FileInputStream is = getActivity().openFileInput(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line).append("\n");
is.close();
catch(OutOfMemoryError om)
om.printStackTrace();
catch(Exception ex)
ex.printStackTrace();
String result = sb.toString();
saveBtn = (Button) view.findViewById(R.id.savechangesbtn);
saveBtn.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Data1 = s1.getText().toString();
Data2 = s2.getText().toString();
Data3 = s3.getText().toString();
Data4 = s4.getText().toString();
Data5 = s5.getText().toString();
Data6 = s6.getText().toString();
try
FileOutputStream fos=getActivity().openFileOutput(file, getActivity().MODE_PRIVATE);
ObjectOutputStream outputStream = new ObjectOutputStream(fos);
outputStream.write(Data1.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data2.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data3.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data4.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data5.getBytes());
outputStream.write(newline.getBytes());
outputStream.write(Data6.getBytes());
outputStream.close();
//Toast.makeText(context,"file saved",Toast.LENGTH_SHORT).show();
catch (FileNotFoundException e)
e.printStackTrace();
catch (IOException e)
e.printStackTrace();
try
FileInputStream inputStream = getActivity().openFileInput(file);
BufferedReader r = new BufferedReader(new InputStreamReader(inputStream));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null)
total.append(line);
r.close();
inputStream.close();
Log.d("File", "File contents: " + total);
catch (Exception e)
e.printStackTrace();
);
如果遇到任何错误,请写下错误的堆栈跟踪。
谢谢
干杯!!简单是最好的!
【讨论】:
我没有任何错误,我遇到了应用启动时出现此文本的问题,可能在关闭应用后此文件也会被删除?仅在 consloe 09-15 07:59:24.604 1647-1661/system_process E/memtrack:无法加载 memtrack 模块 09-15 07:59:24.604 1647-1661/system_process W/android.os.Debug:无法获取内存消耗信息:-1 如果不是普通视图的 loda,还需要检查文件是否在内部存储中以上是关于应用程序启动android时如何从内部存储中读取文件的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Android 10、Target API 29 中从设备的共享存储(根目录)读取现有文件? [复制]