Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7919493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:53:04+00:00 2026-06-03T15:53:04+00:00

I have an Android app where I need to save a file. I create

  • 0

I have an Android app where I need to save a file.
I create the file like this:

FileOutputStream fOut = openFileOutput(fileName, 
                    MODE_WORLD_READABLE);
            osw = new OutputStreamWriter(fOut);

I open it like this:

FileInputStream srcFileStream = openFileInput(fileName);

This works fine with internal memory files.
I’ve added a prefrences to my app, where the user can select to save the file to the sd card instead.
I try this command:

if (Prefs.getCard(this))
        fileName="/sdcard/"+fileName;

Meaning that if the Prefrences is yes, I add “/sdcard/” infront of the file name.
The rest of the code remains the same,

This does not seem to work, as I get an Exception:

File /sd/file1 contains a path seperator

How can I use the same code for both saving to SD card and to internal memory, with just a small modification as i tried above?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:53:06+00:00Added an answer on June 3, 2026 at 3:53 pm

    This should work..

    String baseDir = "";
    String fileName = "myFile.txt";
    FileOutputStream fOut = null;
    if (Prefs.getCard(this)) {
        baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
        File file = new File(baseDir + fileName);
        fOut = new FileOutputStream(file)
    }  else {
        fOut = openFileOutput(fileName, MODE_WORLD_READABLE);
    }
    if (fOut != null) {
        osw = new OutputStreamWriter(fOut);
        ...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my android app I need to do something like this: I have 5
I'm building an Android app and I need to have two different themes for
I'm working with an existing Android app and I need to have a dynamically
I'm developing an android app, and I need to have a json response of
I have an android app for which i need to ensure that it has
I have an Android app that saves a text file directly onto the phone,
I have an Android astronomy app where I need to tint the UI reddish
I am trying to create my first Android app and need some help. Basically
I'm creating an app that will need users to create an account. (Like Facebook
i need to save a simple field to configurate my APP, cause this, i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.