Last active
November 21, 2017 16:28
-
-
Save fida1989/7b856b804d1d5706fd17847263165a11 to your computer and use it in GitHub Desktop.
Reading text file from sdcard
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.hungrydroid.textread; | |
import android.Manifest; | |
import android.content.pm.PackageManager; | |
import android.os.Bundle; | |
import android.os.Environment; | |
import android.support.v4.app.ActivityCompat; | |
import android.support.v4.content.ContextCompat; | |
import android.support.v7.app.AppCompatActivity; | |
import android.util.Log; | |
import android.widget.TextView; | |
import java.io.BufferedReader; | |
import java.io.File; | |
import java.io.FileReader; | |
import java.io.IOException; | |
public class MainActivity extends AppCompatActivity { | |
TextView dispText; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
dispText = (TextView) findViewById(R.id.text); | |
// Here, this is the current activity | |
if (ContextCompat.checkSelfPermission(this, | |
Manifest.permission.READ_CONTACTS) | |
!= PackageManager.PERMISSION_GRANTED) { | |
ActivityCompat.requestPermissions(this, | |
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, | |
0); | |
} else { | |
try { | |
// You can provide the dir location from SD card root | |
String data = getTextFileData("data.txt"); | |
// Display the text contents on a TextView | |
dispText.setText(data); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
@Override | |
public void onRequestPermissionsResult(int requestCode, | |
String permissions[], int[] grantResults) { | |
switch (requestCode) { | |
case 0: { | |
// If request is cancelled, the result arrays are empty. | |
if (grantResults.length > 0 | |
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) { | |
// permission was granted, yay! Do the | |
// contacts-related task you need to do. | |
try { | |
// You can provide the dir location from SD card root | |
String data = getTextFileData("data.txt"); | |
// Display the text conetents on a TextView | |
dispText.setText(data); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} else { | |
// permission denied, boo! Disable the | |
// functionality that depends on this permission. | |
} | |
return; | |
} | |
// other 'case' lines to check for other | |
// permissions this app might request | |
} | |
} | |
public String getTextFileData(String fileName) { | |
// Get the dir of SD Card | |
File sdCardDir = Environment.getExternalStorageDirectory(); | |
// Get The Text file | |
File txtFile = new File(sdCardDir, fileName); | |
// Read the file Contents in a StringBuilder Object | |
StringBuilder text = new StringBuilder(); | |
try { | |
BufferedReader reader = new BufferedReader(new FileReader(txtFile)); | |
String line; | |
while ((line = reader.readLine()) != null) { | |
text.append(line + '\n'); | |
} | |
reader.close(); | |
} catch (IOException e) { | |
Log.e("C2c", "Error occured while reading text file!!"); | |
} | |
return text.toString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment