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

  • Home
  • SEARCH
  • 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 6933131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:47:35+00:00 2026-05-27T11:47:35+00:00

This is my first post here, and i should advise that my knowledge is

  • 0

This is my first post here, and i should advise that my knowledge is at best limited!

I am trying to create an app for my android phone.

I need to eventually output values input by the user to a file, most probably a txt file, that can be retreived direct from the phone when connected via a usb cable.

However, I am struggling to understand how to achieve this.

From what I understand that I need to ensure the card is mounted,
To get the path,
and then use FileOpenStream(), with FileWriter
to ‘load’ strings into the file (sorry not sure on the proper way to desribe that, or anythign else for that matter)

I have also allowed Permission for in the manifest to write to the sdcard. However, all the exmaples i can find seem to not relate to the sdcard. any advise, and or pointing to tutorials gratefully recieved.

my current code, is all over the place as i have confused myself to much, sorry.

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media    
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media   
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need   
    //  to know is we can neither read nor write  
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}

if (mExternalStorageAvailable == true && 
        mExternalStorageAvailable == true) {
    //Environment.getExternalStorageDirectory()  gets root path to SD card
    // FileOutputStream to output somehow
    File path = Environment.getExternalStorageDirectory();
    OutputStream out = null;
    try {
        //File path = Environment.getExternalStorageDirectory();
        //FileOutputStream f = new FileOutputStream(new File(path + "/DCIM", "fileName"));

        out = new BufferedOutputStream(new FileOutputStream(file));

        OutputStreamWriter osw = new OutputStreamWriter(f);

        osw.write("test txt");
        osw.flush();// ensure that everything is really written out and close
        osw.close();
        Toast.makeText(getBaseContext(), "path = " + path, Toast.LENGTH_LONG).show();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "File not FOund", Toast.LENGTH_LONG).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "IOException", Toast.LENGTH_LONG).show();
    }
}

EDIT:

Here is the XML File (hopefully), to see if the FileNotFound exception is be caused by it.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.androidbook.epcsn"
  android:versionCode="1"
  android:versionName="1.0">
<uses-sdk android:minSdkVersion="9" />
<permission></permission>

<application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true" android:permission="android.permission.WRITE_EXTERNAL_STORAGE">
  • 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-05-27T11:47:36+00:00Added an answer on May 27, 2026 at 11:47 am

    I cleaned up your code a bit and changed a few lines and I think it does what you want now. I used a BufferedWriter to write to the sdcard.
    You can read more about the BufferedWriter I used here

        boolean mExternalStorageAvailable = false;
        boolean mExternalStorageWriteable = false;
    
        String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state)) {  
            // We can read and write the media    
            mExternalStorageAvailable = mExternalStorageWriteable = true;
        } 
        else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { 
            // We can only read the media   
            mExternalStorageAvailable = true;    
            mExternalStorageWriteable = false;
        } else {   
            // Something else is wrong. It may be one of many other states, but all we need   
            //  to know is we can neither read nor write  
            mExternalStorageAvailable = mExternalStorageWriteable = false;
        }   
    
        if ( mExternalStorageAvailable == true && mExternalStorageAvailable == true){
    
            File mFile = new File(Environment.getExternalStorageDirectory(), "/DCIM/filename.txt");
            try {
                BufferedWriter buf = new BufferedWriter(new FileWriter(mFile, true)); 
                buf.write("test txt");
                buf.close();
                Toast.makeText(getBaseContext(), "path = "+mFile, Toast.LENGTH_LONG).show();
    
    
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "File not FOund", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(getBaseContext(), "IOException", Toast.LENGTH_LONG).show();
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here and I wanted to get some input from
This is my first post here. I have a problem. I need to take
this is my first post here on stackoverflow and am very impressed by the
Well, this is my first post here and really enjoying the site. I have
My first post here, so i hope this is the right area. I am
This is my first time here so I hope I post this question at
Frequent visitor but first post here on StackOverflow, I'm hoping that you guys might
First post here so sorry if I seem like a newb, I am trying
First post, so here goes. I'm writing a script that does intelligent search and
this is my first post, and it covers something which I've been trying to

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.