0

Firstly I'm crap at english, I hope you'll understand everything. I'm trying to create an app that create a simple file "lol.txt" which contains "hello world!". i'm a bit noob, I tried to follow tutos on internet.

The thing is, I succeed to reach the debug line "file created?" but I can't find the folder of my app nor the .txt file that I created.

Is my code wrong? I can't find any folder of my app when I upload it on my phone with eclipse.

MainActivity.java

package com.example.brosselesdents;

import java.io.File;
import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends Activity implements OnClickListener{

int compteur = 0;

TextView text1;
String fileName = "lol.txt";
FileOutputStream outputStream;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    text1 = (TextView)findViewById(R.id.textView1);

    Button button1;
    button1 = (Button)findViewById(R.id.button1);

    final String string = "Hello world!";
    button1.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) {
            try {
                outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
                outputStream.write(string.getBytes());
                outputStream.close();
                text1.setText("File created?");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    });

}   


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


@Override
public void onClick(View v) {
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.brosselesdentsbatards.MainActivity" 

android:id="@+id/layout1">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="91dp"
    android:text="Button" />

</RelativeLayout>

If you have any idea, like the default folder (I searched on "android/data/com.example.(...)/" ) or maybe a mistake in my code?

Thank you very much. I apreaciate your help.

Axel Syntes
  • 5
  • 1
  • 4

1 Answers1

0

I have done this for some other reason, but by sharing this i hope you can adjapt it and try it out on your own... I hope it will work. Modify as per your requirement.

I am showing a sample log file.txt to create in sd card and reading that file. Here is code.

MainActivity:

public class MainActivity extends Activity {
TextView tv;
Button b;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    tv=(TextView) findViewById(R.id.album_title);
    b=(Button)findViewById(R.id.button1);

    tv.setText("TESTING TO INSERT A LOG TXT IN SD CARD");
    //I am just passing this text file in Log
    String text = tv.getText().toString();
    MyLog.i("Info", text);
    MyLog.e("Error", text);
    MyLog.d("Debug", text);
    b.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {

            File sdcard = Environment.getExternalStorageDirectory();

            //Get the text file
            File file = new File(sdcard,"a.txt");

            //Read text from file
            StringBuilder text = new StringBuilder();

            try {
                BufferedReader br = new BufferedReader(new FileReader(file));
                String line;

                while ((line = br.readLine()) != null) {
                    text.append(line);
                    text.append('\n');
                }
                br.close();
            }
            catch (IOException e) {
                //You'll need to add proper error handling here
            }

            //Find the view by its id
            TextView tv = (TextView)findViewById(R.id.textView1);

            //Set the text
            tv.setText(text);
        }
    });

    }
}

MyLog :

    public class MyLog {

    public static void i(String TAG, String message){

        // Printing the message to LogCat console
        Log.i(TAG, message);

        // Write the log message to the file
        insertToSdCard(message);
    }

    public static void d(String TAG, String message){
        Log.d(TAG, message);
        insertToSdCard(message);
    }
    public static void e(String TAG, String message){
        Log.e(TAG, message);
        insertToSdCard(message);
    }
    // creating method to insert into sd card.
    private static void insertToSdCard(String text) {

        File logFile = new File("sdcard/a.txt"); 
        if (!logFile.exists()) { 
            try { 
                logFile.createNewFile(); 
            }catch (IOException e){ 
                e.printStackTrace(); 
          } 
       } 
       try { 
           BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));  
           buf.append(text); 
           buf.newLine(); 
           buf.close(); 
       } catch (IOException e) { 
           e.printStackTrace(); 
       } 
    }
}

and xml is here:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="2dip">
 <TextView
     android:id="@+id/album_title"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"                              
     android:textSize="17sp"
     />

 <Button
     android:id="@+id/button1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignLeft="@+id/album_title"
     android:layout_below="@+id/album_title"
     android:layout_marginLeft="44dp"
     android:layout_marginTop="129dp"
     android:text="Read sd card" />

 <TextView
     android:id="@+id/textView1"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignBottom="@+id/button1"
     android:layout_alignLeft="@+id/button1"
     android:layout_marginBottom="71dp"
     android:text="Display text from sd card" />

</RelativeLayout>

Don't forget to give permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Happy coding...

Martin Frank
  • 3,445
  • 1
  • 27
  • 47
Shadow
  • 6,864
  • 6
  • 44
  • 93