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 9190727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:31:43+00:00 2026-06-17T20:31:43+00:00

I have been experimenting with writing to sdcard storage in my Android application (Target

  • 0

I have been experimenting with writing to sdcard storage in my Android application (Target API 11)

I have been successful in creating the Directory in the following code called “BPAExp_data” but it does not create a new file at all. I’m stumped as to why since it does not show up in the logcat!!.

Here is my onCreate() method

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_text_logger);
    String data = "asdfghjkl.."

    //The filename of the log file
    filename = getIntent().getExtras().getString("Filename");

    //Save Test data button
    Button saveData = (Button)findViewById(R.id.saveDataButton);
    saveData.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
        // Save Data in file and go back to home screen
        addtoKeyLogFile(data);              

        //Make intent for the Main Activity screen
        Intent main_intent = new Intent(TextLogger.this, MainActivity.class);
        startActivity(main_intent);
        }
    });

This is my method called addKeyLogFile()

    public void addtoKeyLogFile(String data){       
    FileOutputStream f = null;
    File data_file;
    try {
        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/BPAExp_data");
        dir.mkdirs();
        data_file = new File(dir, filename);
        f = new FileOutputStream(data_file);
        // if file doesn't exists, then create it
        if (!data_file.exists()) {
            data_file.createNewFile();
            Toast.makeText(TextLogger.this, "File GOT CREATED!", Toast.LENGTH_SHORT).show();
        }

        // get the content in bytes
        byte[] dataInBytes = data.getBytes();

        f.write(dataInBytes);
        f.flush();
        f.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
        Log.d("TextLogger onCreate: ", e.getMessage());
    } catch (IOException e) {
        e.printStackTrace();
        Log.d("TextLogger onCreate: ", e.getMessage());
    } finally {
        try {
            if (f != null) {
                f.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

I have Located that the problem lies in or near the

    f = new FileOutputStream(data_file);

because when I put a toast message after that it is never executed BUT if I put a toast before it, it is executed! Also the intent after that works perfectly fine!
But I do NOT understand, why is it not working?

Thanks for any help anyone can provide!

  • 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-17T20:31:44+00:00Added an answer on June 17, 2026 at 8:31 pm

    File has multiple constructors. One take as parameters a File and String:

    File dir = new File (sdCard , "BPAExp_data");
    dir.mkdirs();
    data_file = new File(dir, filename);
    

    What’s the value of filename ?

    remove those lines:

       if (!data_file.exists()) {
              data_file.createNewFile();
              Toast.makeText(TextLogger.this, "File GOT CREATED!", Toast.LENGTH_SHORT).show();
       }
    

    File will be created when you stop writing the file.

     f.close();
    

    you call it twice. Leave it only inside the finally block

    All in all, are you sure that you have something to write?

    byte[] dataInBytes = data.getBytes();
    

    is the length of dataInBytes > 0 ?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been experimenting with writing applications that use a local SQL Database to
I have been experimenting with Firefox's Audio API to detecting silence in audio. (The
I have been experimenting with creating my own gradients for the UI in my
I'm just starting to dive into some basic Android development and have been experimenting
I have been experimenting a lot lately with Windows 8, writing C# XAML Metro
I have been experimenting a lot with setInterval and clearInterval recently, for creating my
I have been experimenting with a jquery script that I found on the following
I have been experimenting with a lot of web development apps like Drupal, Moodle,
I have been experimenting with sending messages from two .NET Windows Forms applications using
I have been experimenting with TMask in Delphi 2010 and it seems to work

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.