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
Related
I am using the first snippet to write a file.
String fileName = "Test6.txt";
String outputString="Text for File";
try {
FileOutputStream outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(outputString.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
And the second to read it.
try{
FileInputStream InputStream = openFileInput("Text6.txt");
InputStreamReader inputStreamReader = new InputStreamReader(InputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String lineData = bufferedReader.readLine();
}catch(FileNotFoundException ex)
{
Log.d(TAG, ex.getMessage());
}
catch(IOException ex) {
Log.d(TAG, ex.getMessage());
}
But I can't read it, I get:
java.io.FileNotFoundException: /data/user/0/com.example.android.buildingmarque2/files/Text6.txt (No such file or directory)
I can also get a list of the files and Test6.txt is in the list.
Also, Android Studio Device File Explorer shows it.
It could be a problem with the path.
Device Explorer, "Copy Path" gives me
/data/data/com.example.android.buildingmarque2/files/Test6.txt
But the Log says:
/data/user/0/com.example.android.buildingmarque2/files/Text6.txt
I'm confused?
Typo. One is "Text6" the other is "Test6". Use a constant for both names to avoid this in the future
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.
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
I am trying to read the user entered logs using log.i() programmatically as follows,
try {
Log.i("logs", "inside log.java");
String filter = "logcat ";
filter += "-d ";
filter += "logs:I";
// String[] command = new String[] { "logcat", "-s" , "logs:"+filter
// };
Process process = Runtime.getRuntime().exec(filter);
BufferedReader bufferedReader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
log = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
log.append(line);
log.append("\n");
}
clear = (Button) findViewById(R.id.clear);
clear.setOnClickListener(onCleared);
tv = (TextView) findViewById(R.id.logview);
tv.setText(log.toString());
} catch (IOException ex) {
Log.e("Logging", ex.toString());
}
// convert log to string
final String logString = new String(log.toString());
// create text file in SDCard
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/myLogcat");
dir.mkdirs();
File file = new File(dir, "logcat.txt");
try {
// to write logcat in text file
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
// Write the string to the file
osw.write(logString);
osw.flush();
osw.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
but it simply shows a black screen . but when i try to run the command in adb shell it works . my adb code is,
adb logcat -s logs:I
I was facing a similar problem while trying to log files to text programmatically. I think what's happening here is that the log reading process is blocking the UI (main) thread, resulting in the black screen.
Try putting the entire log reading process into an ASyncTask. That made it work for me!
Did you add this to your manifest file?
<uses-permission android:name="android.permission.READ_LOGS" />
I tried to find this but couldn't find and results. Apologies if this is a duplicate.
I would like to have android logs written to a file. But i do not wish them to be written to the file in the end, rather as the log statement is executed, it should be written into this file.
But i do not wish them to be written to the file in the end, rather as the log statement is executed, it should be written into this file.
Then write one or more methods that log to a file, possibly in addition to logging to LogCat.
Logging to files has been done in Java development for ~15 years, and there is plenty of code available to demonstrate it.
You can try this code!
public static void printLog(Context context){
String filename = context.getExternalFilesDir(null).getPath() + File.separator + "my_app.log";
String command = "logcat -d *:V";
Log.d(TAG, "command: " + command);
try{
Process process = Runtime.getRuntime().exec(command);
BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
try{
File file = new File(filename);
file.createNewFile();
FileWriter writer = new FileWriter(file);
while((line = in.readLine()) != null){
writer.write(line + "\n");
}
writer.flush();
writer.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
catch(IOException e)
{
e.printStackTrace();
}
}
Ref: How to keep on appending logs while writing into File in SDCard?
i use this:
Logger logger=Logger.getLogger("myLoggerName");
logger.setLevel(Level.ALL);
File logFileDirectory=new File(getExternalFilesDir(null),"myLogDirectory");
addFileHandler(logger,logFileDirectory,"myLogFile");