Android read external storage not working in Android Kitkat - android

I have the following code for reading a text file from storage:
//Get the text file
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"CONFIG.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');
}
}
catch (IOException e) {
Toast.makeText(MainActivity.this,
"Configuration file not found!!", Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this,
text.toString(), Toast.LENGTH_SHORT).show();
}
This code is working fine in jellybean device(Micromax canvas4). But it is not reading from a kitkat device(Moto G).It is getting the toast message "Configuration file not found" I have tried a lot. But not working. How can I figure out??

Problem solved by adding permissions READ and WRITE SD card!! Thanks guys for all support

Related

Unable to read .txt file which stored on local storage

I am trying to read file.txt file which stored on my SD Card but am unable read it on my app. I provided
"android.permission.READ_EXTERNAL_STORAGE"
permissions on manifest. Not sure where it went wrong. Please see my code below what i tried.
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.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.text_view);
//Set the text
tv.setText(text);

Opening a textfle and pushing it to parse cloud (android)

I have a textfile in /sdcard/applit/mytext.txt
I want to push it to parse cloud.Googled a lot but No profit.Please explain completely.Thanx.
private void txtPusher(File dir) throws IOException {
File outputFile;
outputFile=newFile(dir,"MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt");
byte [] b;
b=FileUtils.readFileToByteArray(outputFile);
file=new ParseFile("MyText\t"+ParseUser.getCurrentUser().getUsername()+".txt",b);
file.saveInBackground();
TextPusher Tpusher=new TextPusher(file);
Tpusher.execute();
}
Here dir is the directory I am passing to txtPusher function.I want to know wether output file is that file which I am going to push or another directory or it is creating a new file.but my file is not getting pushed.If i am wrong please share the right way to push the textfile
You can read the contents of a text (.txt) file using the following:
private String readFile(String fileName) {
//Find the directory for the SD Card using the API
File sdcard = new File(Environment.getExternalStorageDirectory() + File.separator + "Inventory_Files/Version/");
// Get the text file
File file = new File(sdcard, fileName);
// 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');
}
} catch (IOException e) {
//You'll need to add proper error handling here
}
return text.toString();
}
As for sending this data to the cloud, this looks to be very well documented on their site.

Can't access file in assets subfolder

Im developing an app using Andriod Studio and I´ve got some problemns accessing my assets subfolders.
When I try to access a file, say file1, in the assets root folder, like this
AssetManager assetMng = getAssets();
InputStream textoInput;
String path = "file1.txt";
try {
textoInput = assetMng.open(path);
BufferedReader r = new BufferedReader(new InputStreamReader(textoInput));
StringBuilder total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
total.append(line);
}
} catch (IOException e){
lista.add("Exception: " + e.toString());
e.printStackTrace();
}
I get the text successfuly. But when I change "file1.txt" to "sub\file2.txt" I get an file not found execption.
Anyone has any idea what is going on? Am I missing something?
Thanks!
Android is linux based - use a forward slash not backslash. So, sub/file2.txt

Can i read a file from SmsManager

I copy+paste a *.txt file into the folder /frameworks/opt/telephony/src/java/android/telephony. Now I want to read it from SmsManager, but when I run the "emulator + adb logcat" it show me that it cant find the file.
Both files SmsManager.java and textfile.txt are in the same folder.
My code inside SmsManager is:
try {
FileInputStream fstream = new FileInputStream("textfile.txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String line;
// read every line of the file into the line-variable, on line at the time
while (( line = br.readLine()) != null) {
Log.i(TAG, line+"##########################");
}
in.close();
} catch (java.io.FileNotFoundException e) {
Log.i(TAG, "File didnt found");
} catch (java.io.IOException e) {
Log.i(TAG, "File didnt found");
}
What is wrong, or where must I save the file to find it?
You will have to modify device.mk like below to copy from your source code to Android file system
PRODUCT_COPY_FILES := frameworks/abc.txt:system/abc.txt
In your code you will have to access /system/abc.txt

Character count from a text file

i am trying to count the number characters in a text file which i have retrieved the SD card need some advice here am new to android programming
using this code to retrieve the text file from the sd card
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();
//Get the text file
File file = new File(sdcard,"file.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');
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}
//Find the view by its id
TextView tv = (TextView)findViewById(R.id.TextView01);
//Set the text
tv.setText(text);
Assuming that you have already opened the file and haven't encountered any exceptions in it, I would suggest using this, instead of what you're using:
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line="";
int c,counter=0;
while ((c = br.read()) != -1) {
line+=(char)c;
counter++;
}
text.append(line);
}
catch (IOException e) {
//You'll need to add proper error handling here
}
Here, I haven't used text.append("\n") because your file will already be having line break characters.
In the enc counter will be having the text count, including spaces and special characters like '\n'

Categories

Resources