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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:02:24+00:00 2026-06-17T22:02:24+00:00

Im still very new to Android development, so I apologize in advance if my

  • 0

Im still very new to Android development, so I apologize in advance if my question seems silly.

In my application I have one button. When the button is clicked it attempts to see if the application has its own folder on the internal storage, if not it creates a folder, then it creates a file called output.txt, then it writes system information to the output.txt, then it attempts to write all lines containing “SIP_MESSAGE” from the logcat into the output.txt, it then emails the default email address.


EDIT

After a few days of toiling I managed to put it all together. Please read the answer below for everything in detail.

  • 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-17T22:02:25+00:00Added an answer on June 17, 2026 at 10:02 pm

    After a few days of research and a countless number of guess and checks, I finally figured everything out. I want to take this time to actually explain everything, in case anyone comes across this and is having the same problems that I had. Hopefully everything you are looking for is right here, and I gave a better explanation then the 100’s of other sites that you (and I) had visited previous to this.

    First topic is the difference between internal and external storage (it’s not the difference between sdcard and not sdcard).

    Internal storage is something that no one can see or get to but your application. If you create a file or folder on the internal storage, you cant use a file browser (unless your rooted) or your computer to see what you’ve created, it is completely inaccessible from outside your application.

    Public folders such as Documents/Downloads/Music/Ringtones/etc. are technically on you external storage. You need permissions to write and read from it. This is where I was getting confused. I thought only sdcards counted as external storage, external storage is something you can manually get to from a computer or file browser whether its on an sdcard or not.

    To create a file on the internal or external storage you do not need to use mkDir(). Anyone that says you do, is overly complicating things. You can actually create any text file anywhere on the system just from the code:

    PrintWriter osw = new PrintWriter(Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).toString() + "/output.txt");
    

    This creates a text file in the download directory, whether it existed there or not first. You can also use getDataDirectory() or wherever else you want to create the file.

    Next Logcat, like what the other people were pointing out, I was trying to read from the logcat as it was being created. There is no end to the logcat, so in effect, my application hung because it was constantly looking for more to write. An easy way around this is to use the -d feature of logcat. What that does is it just takes everything up to the point where -d was entered (which was exactly what I wanted), then it stops, then you can put it into a buffer and get the output with no hanging.

    Finally, attaching a file to an email intent. This one was tricky because there were a few different areas that ended up giving me problems. In short, if you are receiving the error, “Couldn’t show attachment”, it means one of two things – 1.) you are trying to attach a file from the internal memory (remember, no other programs are allowed to access the internal memory, even gmail) or 2.) you are not using getAbsolutePath(). I found quite a few people that said you can’t attach a file using uri.parse() and the you have to use uri.fromFile(), that is wrong, attached I show you how to attach a file and not get an error.

    I hope this code helps you, and I hope you do not spend 1/10th of the time I did trying to figure this stuff out.

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.PrintWriter;
    import java.util.Calendar;
    
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.app.Activity;
    import android.content.Intent;
    import android.view.Menu;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;
    
    public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button mailb = (Button)findViewById(R.id.bmail);
        final TextView confirmation = (TextView)findViewById(R.id.Confirmation);
        mailb.setOnClickListener(new View.OnClickListener() {   
        @Override
    
        public void onClick(View v) {
            try {     
                PrintWriter osw = new PrintWriter(Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).toString() + "/output.txt"); //This creates a file in my public download directory
                   osw.println("Output Log: Report Tool");
                   osw.println("Date: " + java.text.DateFormat.getDateTimeInstance().format(Calendar.getInstance().getTime())); 
                   osw.println("------------------------------------");
                   osw.println("Manufacturer: " + android.os.Build.MANUFACTURER);
                   osw.println("Model: " + android.os.Build.MODEL);
                   osw.println("Serial: " + android.os.Build.SERIAL);
                   osw.println("BootLoader: " + android.os.Build.BOOTLOADER);
                   osw.println("Build ID: " + android.os.Build.FINGERPRINT);
                   osw.println("------------------------------------");
                  try { 
                      Process p = Runtime.getRuntime().exec("logcat -d -v long"); //This gets the dump of everything up to the button press
                      BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                      String line = null;
                      while ((line = reader.readLine()) != null) {
                        if(line.toString().contains("SIP_MESSAGE")){ //This parses out everything but SIP Messages
                        osw.println(line); }}}
                  catch (IOException e1) {confirmation.setText(e1.getMessage()); }
    
                   osw.flush();
                   osw.close();
    
            } catch(Exception e){ confirmation.setText(e.getMessage()); }
    
            String attach = Environment.getExternalStoragePublicDirectory(DOWNLOAD_SERVICE).getAbsolutePath() + "/output.txt"; //This is where you need to use the absolute path!!
            Intent i = new Intent(Intent.ACTION_SEND);
            i.setType("message/rfc822");
            i.putExtra(Intent.EXTRA_EMAIL  , new String[]{"MyEmail@Email.com"});
            i.putExtra(Intent.EXTRA_SUBJECT, "Error Report.");
            i.putExtra(Intent.EXTRA_TEXT   , "Please see the attached file...");
            i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + attach)); //This is where you attach the file 
            try {
                startActivity(Intent.createChooser(i, "Send mail..."));}
            catch (android.content.ActivityNotFoundException ex) {
                confirmation.setText("There is no Email Client installed on this device.");}                
        }
    });
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
    }    
    

    And finally, the permissions I used for this was READ_LOGS, WRITE_EXTERNAL, READ_EXTERNAL.

    I hope you’ve enjoyed, and good luck.

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

Sidebar

Related Questions

I'm still very new to application development, so this is probably a very stupid
I have been learning android development and I am still new to this. I
I am hoping someone can help. Still very new to Android/Java. I have an
I'm very new to Android development and am still struggling with some really basic
I'm still very new to Android, but I am trying to keep up by
I'm still new to android so this is probably a pretty simple question, thanks.
I am very new to android development so pardon my ignorance. I am simply
I am new to android development but in the past few weeks I have
I'm still very new at programming, and our local SSIS genius isn't here today
I am still very new to the MVC framework, but I managed to create

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.