Android - Android Studio                             Home : www.sharetechnote.com

 

 

 

 

Text File Operation

 

 

< AndroidManifext.xml > ---------------------------------------------------------------

 

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.example.android.location_01">

 

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

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

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

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

 

    <application

        android:allowBackup="true"

        android:icon="@mipmap/ic_launcher"

        android:label="@string/app_name"

        android:roundIcon="@mipmap/ic_launcher_round"

        android:supportsRtl="true"

        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

 

 

</manifest>

 

 

< MainActivity.java > ---------------------------------------------------------------

 

package com.example.android.location_01;

import android.Manifest;

import android.content.Context;

import android.content.pm.PackageManager;

import android.os.Bundle;

import android.widget.TextView;

import android.view.View;

import android.location.Location;

import android.location.LocationManager;

import android.location.LocationListener;

import android.support.v4.app.ActivityCompat;

import android.support.v7.app.AppCompatActivity;

import android.os.Environment;

import android.util.Log;

 

import java.io.File;

import java.io.OutputStreamWriter;

import java.io.FileOutputStream;

import java.io.IOException;

 

 

public class MainActivity extends AppCompatActivity  implements LocationListener {

 

    TextView CoordList;

    String strTmp;

 

    //private final Context context;

    static final int REQUEST_LOCATION = 1;

    LocationManager locationManager;

    Location coord;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        CoordList = (TextView)findViewById(R.id.txtCoord);

        locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);

 

        GetPermission();

 

        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,

                2000, 1, this);

 

    }

 

    @Override

    public void onLocationChanged(Location location) {

        UpdateLocation();

    }

 

    @Override

    public void onStatusChanged(String provider, int status, Bundle extras) {

 

    }

 

    @Override

    public void onProviderEnabled(String provider) {}

 

    @Override

    public void onProviderDisabled(String provider) {}

 

    public void UpdateLocation()

    {

 

        GetPermission();

 

        coord = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

 

        if (coord != null) {

            double lattitude = coord.getLatitude();

            double longitude = coord.getLongitude();

            CoordList.setText("Lat = " + String.format("%5.2f",lattitude) +

                              ",Long = " + String.format("%5.2f",longitude));

        } else {

            CoordList.setText("No Location data available");

        };

 

    }

 

    public void GetPermission()

    {

 

        if(ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)

                != PackageManager.PERMISSION_GRANTED &&

                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)

                        != PackageManager.PERMISSION_GRANTED){

            ActivityCompat.requestPermissions(this,

                    new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);

            CoordList.setText("Premission Request");

        } else {

            CoordList.setText("Already Got the Permission. So No Request");

            //CoordList.setText("");

        };

 

 

    }

 

    public void SaveLocation()

    {

 

 

        final File path =

                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM + "/Location/") ;

 

        Log.d("INFO ", path.toString());

 

        if(!path.exists())

        {

             Log.d("INFO ", "Creating a Directory :" + path.toString());

             path.mkdirs();

 

        }

 

        final File file = new File(path, "coord.txt");

 

        Log.d("INFO ", file.toString());

 

        try

        {

            if( file.exists() == false ) {

                file.createNewFile();

                Log.d("INFO ", file.toString() + " is created ");

            };

 

            FileOutputStream fOut = new FileOutputStream(file,true);

            OutputStreamWriter fOutWriter = new OutputStreamWriter(fOut);

            fOutWriter.append( CoordList.getText() + "\n");

 

            fOutWriter.close();

 

            fOut.flush();

            fOut.close();

        }

        catch (IOException e)

        {

            Log.e("Exception", "File write failed: " + e.toString());

        }

 

 

    }

 

    public void btnUpdate_Click(View v) {

 

        UpdateLocation();

        SaveLocation();

    }

 

}

 

 

Permitting File Creation / Access

 

 

 

 

 

 

 

 

Running the App

 

 

 

Checking the Contents of the File