I'm trying to create a file in the internal storage
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
But when i search in /data/data/ i don't find the app dir to see the file presence...why? I try with a context.getApplicationInfo().dataDir() but it don't work it give error. How can i do? What is/are my mistake(s)?
Try something like this,
File path = getFilesDir();
File file = new File(path, filename);
FileOutputStream outputStream = new FileOutputStream(file);
outputStream.write(string.getBytes());
outputStream.close();
you should probably do a google search before posting.
The file will be located in
data->data->package name->files->filename
File Explorers have no access to the /data directory.
With getFilesDir() you will know the directory where you put your file.
outputStream = context.getFilesDir().openFileOutput(filename, Context.MODE_PRIVATE);
getFilesDir() returns the Absolute path to the file .
Read here
Related
Given this code:
final String dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS) + "/";
String file = dir + "info.txt";
File newfile = new File(file);
//Environment.getExternalStorageDirectory().toString()= /storage/emulated/0
String msgData="p1000";
FileOutputStream outputStream;
try {
newfile.createNewFile();
outputStream = openFileOutput(file, Context.MODE_PRIVATE);
outputStream.write(msgData.getBytes());
outputStream.close();
} catch (Exception e) {
e.toString();
//e.printStackTrace();
}
When I open file /storage/emulated/0/Documents/info.text, I find it empty while it should have the string "p1000";
why is that?
note: I do not have external storage (external sd card).
thanks.
Do you have WRITE_EXTERNAL_STORAGE Permission in your Manifest?
Try this: FileOutputStream outputStream = new FileOutputStream(file);
If you wanna use internal storage you should not pass a path pointing at the external storage (roughly sd card). In you try-catch block use this line instead:
outputStream = openFileOutput("info.txt", Context.MODE_PRIVATE);
The file will be saved in the directory returned by Context.getFilesDir().
Where is the android data folder exists ?since i have to save large files and users should not able to see them ,i think to use data folder.But i don't know where is located in android phone.does it is on sdcard or not ?
You should use Internal storage
Internal storage is best when you want to be sure that neither the user nor other apps can access your files.
Sample code:
File file = new File(context.getFilesDir(), filename);
String filename = "myfile";
String string = "Hello world!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
public File getTempFile(Context context, String url) {
File file;
try {
String fileName = Uri.parse(url).getLastPathSegment();
file = File.createTempFile(fileName, null, context.getCacheDir());
catch (IOException e) {
// Error while creating file
}
return file;
}
For more details, please refer here.
Use the below to get the path of of data directory
File f = Environment.getDataDirectory();
System.out.println(f.getAbsolutePath());
I'm trying to recreate a simple text editing Android app and so far I have this code to save the file:
String filename = "myfile";
String content = edit.getText().toString();
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename,
Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
However I have no idea where in the internal storage this is being saved. How can I access/look at this directory to confirm the file has actually saved. I cant seem to find the file in File explorer in Eclipse (Heck, I can't even find the app name).Also, how would I go about loading this file once it is located? Thanks.
openFileOutput(filename,
Context.MODE_PRIVATE); is application private directory output stream api. Your file will saved in your /data/data/your_packagename directory.
This is my application memory path /data/data/com.myexample.folder/files and it works fine but when I create a new directory like this /data/data/com.myexample.folder/files/photos,
it is not created and I wonder what's wrong? How do I create a new folder inside application.
public void loadFeed();
String file paths="data/data/com.myapplication.myfolder/files";
File outputFiles= new file(filePaths);
File files1=outputFile1.listFiles();
For your own directory in file storage:
FileOutputStream outStream = null;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/camtest");
dir.mkdirs();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
For saving in sub directory inandroid public folders say DCIM:
use File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
Mainly, make sure that your app has the storage permission enabled:
Go to Device Settings>Device>Applications>Application Manager>"your app">Permissions>Enable Storage permission!
Call openFileOutput() with the name of the file and the operating mode.
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
just use this way:
public void createFile(Context c) throws IOException{
String FILENAME = timeStamp();
String string = "hello world!";
FileOutputStream fos = c.openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
}
I made this code to write on a sdcard, now how can I transform it to write into the internal memory ?
//private String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/liste.txt"; Path used to write on sdcard
try{
File file = new File(path);
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write("something");
bw.flush();
bw.close();
catch(Exception e) {...}
In android each application has its own folder under /data/data/package/
this dir is only accessable by your app and the root if the device is rooted
to access this dir and read/write to it, you can use:
getApplicationContext().openFileOutput(name, mode);
and
getApplicationContext().openFileInput(name);
more about this here : Docs
USe this code:
FileOutputStream fos;
fos = context.openFileOutput(FILENAME, Context.MODE_WORLD_READABLE);
fos.write(data.getBytes()); //write to application top level directory for immediate sending
fos.close();
try{
File file = new File("/data/data/com.example.packagename/files/");
BufferedWriter bw = new BufferedWriter(new FileWriter(file,true));
bw.write("something");
bw.flush();
bw.close();
catch(Exception e) {...}
or
//To read file from internal phone memory
//get your application context:
Context context = getApplicationContext();
filePath = context.getFilesDir().getAbsolutePath();
File file = new File(filePath);