I have successfully been able to save a file to my internal storage on android using the following code in java/android:
public void writeToFile(String filename) {
String code = getIntent().getStringExtra("code_full");
String fullCode = code;
try {
FileOutputStream fileOutputStream = openFileOutput(filename + ".cs", MODE_PRIVATE);
fileOutputStream.write(fullCode.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Saved as " + filename, Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I do not know where I can find the saved file in my android's folders (I am trying to find it on my PC with the phone plugged in.)
Anyone know where to find it?
If you saved a file to internal storage and want a String of the path to the file, you would do getFilesDir().getAbsolutePath().
Related
i have saved some data in a .txt in the app file directory using this way, the name is in FILE_NAME variable which is string.
public void save()
{
mEditText = findViewById(R.id.data);
String text = mEditText.getText().toString();
FileOutputStream fos = null;
try {
fos = openFileOutput(FILE_NAME, MODE_PRIVATE);
fos.write(output.getBytes());
Toast.makeText(this, "Saved to " + getFilesDir() + "/" + FILE_NAME,
Toast.LENGTH_LONG).show();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Now i want to get the data from this .txt file and i'm trying to find a way to do it. It has a lot of lines of information in so it will be convenient if i can send the file directly to my email, upload it on google drive or any other way to get it completely.
I write the file to the directory and after 1-2 seconds I remove the battery from the device. After writing, I open the file and output it to the log in order to make sure that it is recorded. So it is recorded successfully. But after turning on the device I open the file and it is empty.
What could be the reason?
Used to read and write FileOutputStream/FileInputStream, FileWriter/FileReader, FileUtils from Apache Commons IO.
public static void storeForReversal(XmlTag sourceTag_TrnAddRq) {
String toStore = sourceTag_TrnAddRq.buildXmlString();
File sdFile = new File(getFilesDir(), "reversal");
try {
FileUtils.writeStringToFile(sdFile, toStore, "US-ASCII");
} catch (IOException e) {
ZLog.add(MyApplication.TAG, "ERROR store file Reversal: ", e);
}
}
public static String restoreForReversal() {
String result = "";
File sdFile = new File(getFilesDir(), "reversal");
try {
result = FileUtils.readFileToString(sdFile, "US-ASCII");
} catch (IOException e) {
ZLog.add(MyApplication.TAG, "ERROR restore file Reversal: ", e);
}
return result;
}
Device File Explorer
To ensure that information is saved in a file during an emergency power off, you need to use fo.getFD().sync():
try (FileOutputStream fo = new FileOutputStream(sdFile)) {
fo.write(text.getBytes());
fo.flush();
fo.getFD().sync();
} catch (IOException e) {
Log.e(TAG, "");
}
I am attempting to save to long term file storage in android as well as create a new file in the process. This code keeps crashing with minimal helpful logcat.
Thanks.
public void save (String text) {
FileOutputStream fos = null;
try {
fos = openFileOutput("logfile.txt", MODE_PRIVATE);
fos.write(text.getBytes());
} catch (FileNotFoundException e)
{} catch (IOException e) {
e.printStackTrace();
} finally {
if(fos != null)
{
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
I expect it to create a file called logfile.txt and print text to it but instead it crashes.
Try something alike this, in order to get a FileOutputStream from a File in tmp / private storage:
// File file = File.createTempFile("logfile", ".txt");
File file = new File(getFilesDir(), "logfile.txt");
FileOutputStream fos = new FileOutputStream(file);
The resulting path should be /data/data/tld.domain.package/files/logfile.txt.
file.getAbsolutePath() has the value.
See Save a file on internal storage.
I have a function in an sdk where it strictly uses file from asset folder. I want to use the file from online. When I am passing the filename as example.mbtiles it is searching for the file is Asset folder and if found the app working fine. But when I am trying to get the file from a url such as www.myurl.com/example.mbtiles its taking all the url as string and finding the file in Asset folder which makes the app not working. Here is the function
private File getFile(String url, final Context context) {
if (context != null) {
//we assume asset here
AssetManager am = context.getAssets();
InputStream inputStream;
try {
inputStream = am.open(url);
final File mbTilesDir;
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
|| (!Environment.isExternalStorageRemovable())) {
mbTilesDir = new File(context.getExternalFilesDir(null), url);
} else {
mbTilesDir = new File(context.getFilesDir(), url);
}
return createFileFromInputStream(inputStream, mbTilesDir.getPath());
} catch (IOException e) {
Log.e(TAG, "MBTiles file not found in assets: " + e.toString());
return null;
}
}
try {
return new File(url);
} catch (Exception e) {
Log.e(TAG, "can't load MBTiles: " + e.toString());
return null;
}
}
How can I get the file from online url instead of local asset folder? what changes I need to do in this code? I also want to know, can I override this function in my MainActivity?
Like in the method i have attached i have used practiceData.txt i am getting same results while using just practiceData in file constructor so is it ok to use file without any extension or txt is better?
private void saveData(String data) {
File file = new File(this.getFilesDir(), "practiceData.txt");
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(data.getBytes());
saveStatus = "Data was successfully saved.";
} catch (java.io.IOException e) {
e.printStackTrace();
saveStatus = "Error occurred: " + e.toString();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
It doesn't matter what file extension you use, it just tells the OS how to open the file. So yes, you can use no extension and it will work just as well.
If you intend the file to be opened manually via another application, it may be helpful to use a standard extension however.