Post Top Ad

Post Top Ad

Saturday 8 July 2017

File System



1.activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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.kiran.lesson13_filesystem.MainActivity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:hint="Name"
        android:ems="10"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true"
        android:id="@+id/msg_et"
        android:layout_alignParentEnd="true" />

    <TextView
        android:text="TextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/msg_et"
        android:layout_alignParentStart="true"
        android:layout_marginTop="19dp"
        android:id="@+id/textView"
        android:layout_alignParentEnd="true" />

    <Button
        android:text="Write Data"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView"
        android:layout_alignParentStart="true"
        android:layout_marginTop="31dp"
        android:id="@+id/write_btn"
        android:onClick="writeData"
        android:layout_alignParentEnd="true" />
</RelativeLayout>



MainActivity.java:

package com.example.kiran.lesson13_filesystem;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Environment;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

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

public class MainActivity extends AppCompatActivity {

    private EditText mMsgEt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMsgEt = (EditText) findViewById(R.id.msg_et);

    }

    public void writeData(View view){
        // Here, thisActivity is the current activity
        if (ContextCompat.checkSelfPermission(MainActivity.this,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this,
                    new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
                    200); //Shows Dialog, Once he allows or denies goes to onRequestPermissionResult permission will be called.


        }else{
            //for phones that don't need permission
            writeData();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==200){
            if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                writeData(); //Permission Granted
            }else{
                Toast.makeText(MainActivity.this,"PERMISSION DENIED",Toast.LENGTH_SHORT).show();//Permission Denied
            }
        }
    }

    public void writeData() {
        String data = mMsgEt.getText().toString();
        File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC),"hai.txt");
            try {
                if(!f.exists()) {
                    f.createNewFile();
                }
                FileOutputStream out = new FileOutputStream(f);
                out.write(data.getBytes());
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (Exception e){
                e.printStackTrace();
            }
    }
}

No comments:

Post a Comment