Create TXT file in Android Nougat which is editable - android

I have created txt file which is in Internal Storage. I want to edit the file by going through file manager or any file explorer. I can update the file in Lollipop and below. but can't in Android M and Android N. It gives error as access not allowed.
File root = new File (Envirenment.getExternalStorageDirectory(),"FolderName");
if(!root.exists())
{
root.mkdirs();
}
File gpxFile = new File(root, "FileName.txt");
FileWriter writer = new Filewriter(gpxFile );
writer.append("FileBody");
writer.flush(); writer.close();
It working on all devices including Nougat, but when I going to edit from file Explorer it is not editable in Nougat. is any permission required when file is created, as readable by others?

Related

how can I access files in the internal storage's root directory?

My device has no SD card, but I can access the files of "Internal storage" in Windows explorer, in which there is a "tmp.txt".
The txt file was pasted via Windows explorer, now I want to read this file. My codes are:
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,"tmp.txt");
StringBuilder text = new StringBuilder();
try {
InputStream in = new FileInputStream(file);
...
}
catch (IOException e) {
}
But it's not working, it step to catch exception after new FileInputStream(file);, the debug information logs the file location actually is "/storage/emulated/0/tmp.txt", so it couldn't find the file.
Then I tried put the file in the Download directory using:
File fileDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
File file = new File(fileDir, "tmp.txt");
error log:exception e:
It's not working either, basically they are the same problem. Can I access files in this directory?
Read this: developer.android.com/guide/topics/data/data-storage You don't have the permission to read the storage, the permissions you put are for location services.

Creating folder in the application directory

i have used getFilesDir() to create a folder in the application directory, it gives the path of the applicatoin directory as follows
/data/data/{my application package}/files
but when i use it to create a new folder using
File folder = new File(getFilesDir()
+ "/MyFolder");
if (!folder.exists()) {
folder.mkdir();
}
i don't see any folder. Also when i access in ES Explorer the actual path of the application directory is
/Android/data/{my package name}/files
My question is how to create a folder in the application directory so that it can be deleted automatically on application uninstallation.
Use method Context.getDir() instead. You don't need to invoke mkdirs(), because getDir() will do it automatically.
Quote from the documentation:
Retrieve, creating if needed, a new directory in which the application
can place its own custom data files. You can use the returned File
object to create and access files in this directory. Note that files
created through a File object will only be accessible by your own
application; you can only set the mode of the entire directory, not of
individual files.
Use this by making a use of getDir()
File dir = ctx.getDir("my_sub_dir", Context.MODE_PRIVATE);
File newfile = new File(topDirFile.getAbsolutePath() + File.separator + "new_file_name");
newfile.createNewFile();
BufferedOutputStream fout = new BufferedOutputStream(new FileOutputStream(newfile));

Android: File Location

I am writing to a path mnt/sdcard/foldername. I get no errors and the file is written, but when I browse the internal storage of my phone I can not find the folder. Where would the folder be located? I have a galaxy nexus?
Code is here:
File sdCard = Environment.getExternalStorageDirectory();
File directory = new File (sdCard.getAbsolutePath() + "/statReporter/");
directory.mkdirs();
Log.d("Tag", directory.toString());
//Path and name of XML file
File file = new File(directory, appendTimeStamp("phoneNum") + ".csv");
FileOutputStream fOut = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
//Write to CSV File
osw.write(message);
osw.write(',');
//Flush and close OutPutStreamWriter and close FileOutputStream
osw.flush();
osw.close();
fOut.close();
Log.d("File Logger:", "File write successfully!");
Cant find it in Windows Computer\Galaxy Nexus\Internal Storage but all other folders appear.
I used an app called OI File Managaer and I can view the folder and file on phone but how do I view it through Windows OS?
If you really want to find files on your device, I'd recommend one of the Google Play apps you can find (I personally like ASTRO File Manager) or, from the PC you can use (for instance) Android Commander (which, incidentally, will let you use the same file path structure you'll be using from within your developing environment).
I believe that Android devices will not actually show you all paths and available files when you browse it, for instance, with Windows Explorer.
If you're using Eclipse, in the DDMS perspective you can browse your device's file structure. On your right you have the "File Explorer" and as far as I remember, it's a pretty complete tool.
Don't use /mnt/sdcard for accessing external storage. You can use Environment.getExternalStorageDirectory() to access path to external storage. This may vary from device to device.

android change file executable property

I download a pdf file from server with web service and save it in a file
then i try to open the file by PDFViwer but it dosent open
then i open eclipse file exploer i see the file permission is "-rw-----"
i try to change exec permission but it dosent
File myDir = new File(getFilesDir().getAbsolutePath() + "/aaa.pdf");
byte[] data = Base64.decode(evrakIcerikB64, 0);
boolean exec=myDir.setExecutable(true);
boolean read=myDir.setReadable(true);
boolean wri=myDir.setWritable(true);
how can i change file permission?

Accessing file from android data directory

i want to access the /data/local/tmp for any random access file
(RAF) file how to get the path of that file?
go this way and read the raf file line by line but be assure that the folder have read permission to other user or you can change it from adb shell by using chmod commmand .....
File finalFile = new File("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
For accessing the any file through code , you must know the path of the file.
and then you can use the code:
File finalFile = new File/t("/data/local/tmp", fileName);
RandomAccessFile rafTemp = new RandomAccessFile(finalFile, "r");
"/data/local/tmp" is the path for the file. It can differ file to file. So you must know the path of file before accessing it.

Categories

Resources