Can I write file in root directory android? - android

I have a question that I can write a file in root directory by this code?
when I test on emulator then it can write at path: /data/data/com.example.test/files/g.gc
but i donot know that i can write file on device, because i donot have any device to check it.
public static void saveFile(Context context, String content) {
try {
FileOutputStream fw = context.openFileOutput("g.gc", Context.MODE_PRIVATE);
fw.write( content);
fw.close();
} catch (IOException e) {}
}

No you cannot write to root because you do not have permission to. Your application must have an elevated UID level access to be able to write into the root directory.
If you're an app, don't bother.

Related

Android: Cannot see file saved on external storage

public void write(View view)
{
String state;
File Dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
if (!Dir.exists())
{
Dir.mkdir();
}
File file = new File(Dir,"nahk.txt");
Toast.makeText(getApplicationContext(),file.getAbsolutePath(), Toast.LENGTH_SHORT).show();
String Message = "5nahk";
try {
FileOutputStream FOS = new FileOutputStream(file);
FOS.write(Message.getBytes());
FOS.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I have added write permission in the manifest xml file. This method is called when I press a button. The toast says that the txt file is saved in the Download folder of my Internal Storage (since I don't have an SD card in my LG G3). I open the file location (using FileManager on my LG G3) and there is no "nahk.txt" in that folder. Why can't I see the file?
I'm not familiar with native android development but I encountered something similar using cordova. After saving a file the file was not visible browsing with a usb cable but it was actually there. To see it had to restart my device.
So you could try to restart your device and check if it is still invisible (or create a method which fetches or checks the existence of the saved file to check if it is there).
edit: I learned this by googling so you should find it also
Try this
File Dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Your_Folder_Name";
File file = new File(Dir,"nahk.txt");
this file will save inside of your device storage Your_Folder_Name

Save String to a specific folder on internal storage - Android

I am basically trying to save a String to a specific folder I create on the internal storage of my phone when I click the button save. I am still a noob at these stuff therefore I do not know what to do. I want to access the file using the file manager and not through the application I created.
So I need help with:
1- Create the folder.
2- Save the string to that specific folder.
Any help is appreciated and thank you in advance.
Save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(getApplicationContext(), ("X values: "+mg1.getXs()) ,Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), SENSOR_READING_STRING ,Toast.LENGTH_LONG).show();
String filename = "Data.txt";
String ABCD_STRING = "SENSOR_READING_STRING";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(ABCD_STRING.getBytes());
outputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
By default, when saving to internal storage. The data is private and would only be accessible to the application that saved it. So if you want to be able to open the file through file manager you would need to change this line.
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
Context.MODE_PRIVATE should be either MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE depending on your permission level needed.
More info on openFileOutput flags

Reading File from Internal storage with No Read/Write Permissions?

I have a file on Internal Storage on the path e.g. /data/data/<package name>/folder/myfile
I don't have permissions to access this file but for my implementation I have to read this file. for this purpose I first run su command on the file and set permissions through chmod command to 777and then try to access the file. but still show that file does not exist. Please tell how can I can get this file to read.
Following is what I am trying to do.
public void runChmodAndSU(String filePath)
{
Process chperm;
try {
chperm = Runtime.getRuntime().exec("su");
DataOutputStream os = new DataOutputStream(chperm.getOutputStream());
os.writeBytes("chmod 777 "+ filePath +"\n");
os.writeBytes("exit\n");
os.flush();
chperm.waitFor();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
After running above code I check it this way:
File myFile = new File(filePath);
if (myFile.exists())
{
Log.e("Success!", "myFile Exists");
}
else {
Log.e("Failure!", "myFile Does not Exists");
}
Please tell me how can I read this very file. Is there any other way to access files you don't have permissions to?
You can't nor should you be allowed to access such files without the appropriate permissions set in AndroidManifest.xml for your application.
I can't tell if you were intending this to be a "hack android for me please?" question, or if you just don't know what android's uses-permission is about, but the way your question is worded, you really shouldn't be able to access the file.
If su is working, try chmod -R 777 /data/data/<package name>/

Can I see the file created in Android Tablet by file.createNewFile()?

I created a file by file.createNewFile() command in "data/data/com.android.bonvoyage" folder to test file creation in the internal storage of my android tablet.
I found that the file should be visible when I have root account, but I want to find a way
to see the file created without root permission.
I don't care where the file is created, just want to see and test it on actual tablet.
Can I do that?
The process was successful by
File file = new File("data/data/com.android.bonvoyage/myfile.txt");
boolean tf = false;
if (!file.exists()) {
try {
tf = file.createNewFile();
} catch (IOException ioe) {
Toast.makeText(this, ioe.toString(), 5000).show();
//ioe.printStackTrace();
} finally {
Toast.makeText(this, "File Created? " + Boolean.toString(tf), Toast.LENGTH_SHORT).show();
}
}
With root explorer or any file explorer that supports root you could browse to: data/data/com.android.bonvoyage/myfile.txt and look whether its there or not?
Write the file to the sdcard and then you can see it with the file browser without root permissions.
Use the following code (taking from the android guide) to open a file in the pictures folder for example:
public File getAlbumStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES), albumName);
if (!file.mkdirs()) {
Log.e(LOG_TAG, "Directory not created");
}
return file;
}
The full guide can be found here:
http://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage

Android How to use and create temporary folder

I develop an app which collects some data from internet. Then save it to a temporary folder. To build this app I need to create and access a folder ( just for the purpose of app, not for the user). How can I do it?
this code is to create folder:
File direct = new File(Environment.getExternalStorageDirectory() + "/New Folder");
if(!direct.exists())
{
(direct.mkdir()) //directory is created;
}
try it may help you
File mFile;
onCreate()
mFile= new File(Environment.getExternalStorageDirectory()+"/temp/";
mFile.mkdir();
onDestroy();
mFile.delete();
try out this...
private void makeFolder(){
File root = new File(Environment.getExternalStorageDirectory()
+ File.separator + getString(R.string.folder_name));
boolean mainfolderexist = root.exists();
if (!mainfolderexist) {
try {
if (Environment.getExternalStorageDirectory().canWrite()) {
root.mkdirs();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
All The best
You should really check this other SO answer: https://stackoverflow.com/a/6485850/65716
Aside from the fact that you have to completely manage your use of the space, etc, caching on external storage requires more permission for your app.
See http://developer.android.com/reference/android/content/Context.html#getCacheDir()
"Apps require no extra permissions to read or write to the returned path, since this path lives in their private storage."
For app use only, I would recommend to use Context.getDir() for retrieving the directory if the files is used by our app only and don`t want to be visible to users by file browsers.
// No need to check if exist, created automatically.
File tempRoot = context.getDir("temp", Context.MODE_PRIVATE);
// do something

Categories

Resources