FileInputStream in Fragment giving FileNotFound exception

I'm attempting to save integers on internal storage using a fragment class, and seem to be able to use FileOutputStream to save files, but the program never seems to be able to find them once created.

Here's the relevant Java class:

public View onCreateView(final LayoutInflater inflater, final ViewGroup container,

final Bundle savedInstanceState) {

View root = inflater.inflate(R.layout.fragment_preferences, container, false);

final SeekBar s1 = (SeekBar) root.findViewById(R.id.s1Bar);

Button button = (Button) root.findViewById(R.id.savebutton);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

int spi = s1.getProgress();

String fileContents = String.valueOf(spi);

try {

FileOutputStream outputStream = getActivity().openFileOutput("myText.txt", Context.MODE_PRIVATE);

outputStream.write(fileContents.getBytes());

outputStream.close();

Toast.makeText(getContext(), "File saved!", Toast.LENGTH_SHORT).show();}

catch (Exception e){

e.printStackTrace();

Toast.makeText(getContext(), "File not saved!",...

Read More »

By: StackOverFlow - Thursday, 20 September

Related Posts