Im making an app for editing system files (data/data folder). I know that app needs root permission. How can I move files from one data/data folder to other. I thought that it would be easier to make when executing bash command.
Also maybe you have some useful info or documentation for building apps with root
I tried this
but.setOnClickListener {
root
var testName = "bruh.txt"
var text = "Help me"
File( filesDir , testName).writeText(text)
var dstString = "/data/data/com.test.testinproj/code_cache/"
var srcString = "/data/data/com.test.testinproj/files/bruh.txt"
Runtime.getRuntime().exec("mv \"$srcString\" \"$dstString\"")
I expected that it will create file called bruh.txt in filesDir, and will move it to directory i need
You can copy files in Kotlin without making an operating system exec call.
File(srcString).copyTo(
target = File(dstString, "bruh.txt")
)
You can also copy files recursively, which may be easier, if you have multiple files to deal with.
If you need to make sure the destination file and directory is writable:
val destFile = File(dst, "bruh.txt").apply {
if (!parentFile.canWrite()) {
throw IOException("cannot write to $parent")
} else if (exists() && !canWrite()) {
throw IOException("cannot write to ${this.absolutePath}")
}
}
File(srcString).copyTo(
target = destFile
)
Related
I want to create Folder in below path
Internal storage/Android/media/{package name}/myfolder
Can anyone ome help me to create "myfolder" ?
You can use adb shell to access the shell.
Then you can navigate to the required directory using cd command, then mkdir to create the directory.
Using This Function you can Get your required Result.
private File createDirectory(String dirName) {
File file = new File(getApplicationContext().getExternalFilesDir(dirName) + "/" + dirName);
if (!file.exists()) {
file.mkdir();
}
return file;
}
After a lot of googling, and a lot of tries with "out-of-context" code, I'm begging you to help me.
I'm trying to figure out if it's possible to write on the external storage with Nativescript. I'm not interested to write within the application context. So what the docs shows, it's not what i'm looking for.
I've managed to achieve this from a thread on the Nativescript forum:
android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString();
It works, it gives me a path, but when I have this path I have no clue of what to do with it. How to create a file inside that path, read it etc.
What I need to achieve is to create a folder that both the user and the application can easily access. The user should be able to access this folder with the builtin files explorer.
The application runs on Angular.
I really struggled with this one on Android device and in the end it was due to:
Not making sure the required permissions has been granted by the user in the app
Using "Android File Transfer" on my Macbook to verify the files have been created and to download them
tns info
nativescript 6.0.3
tns-core-modules 6.0.7
tns-android 6.0.2
tns plugins
nativescript-permissions 1.3.7
example code
import * as fs from "tns-core-modules/file-system"
...
// First get the required permissions
// Note: this permissions should also be in your AndroidManifest.xml file as:
// <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
const permissions = require('nativescript-permissions')
permissions.requestPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
.then(() => {
console.log('Required Android permissions have been granted');
})
.catch(() => {
console.error('Required Android permissions have been denied!');
});
// Get the publicly accessable Downloads directory path
const sdDownloadPath = android.os.Environment.getExternalStoragePublicDirectory(android.os.Environment.DIRECTORY_DOWNLOADS).toString()
console.log('sdDownloadPath: ' + sdDownloadPath)
// Get a specific folder in that path (will be created if it does not exist)
const myAppFolder = fs.Folder.fromPath(fs.path.join(sdDownloadPath, 'myApp'))
console.log('myApp path: ' + myAppFolder.path)
// Get a file in that path (will be created if it does not exist)
// Note: In this case we try to get a unique file every time this code is run
let date = new Date()
date = date.toISOString().replace('.', '')
const myFile = myAppFolder.getFile(`myfile_${date}.txt`)
console.log('myFile path: ' + myFile.path)
// Write some data to this new file
myFile.writeText('Hello duder 123')
.then(() => {})
.catch((err) => console.log(`Error writing to file: ${err}`))
// Try and read back the data that was written
myFile.readText()
.then((res) => {
console.log(`Text read back: ${res}`)
}).catch((err) => {
console.log(err.stack);
});
// List all files in the myApp folder
myAppFolder.getEntities()
.then((entities) => {
// entities is array with the document's files and folders.
entities.forEach((entity) => {
console.log(entity)
});
}).catch((err) => {
console.log(err.stack);
});
android file transfer issue
One problem I wasted a lot of time on was that I could see the files with getEntities() but could not see them when using the 3rd party tool "Android File Transfer (AFT)" on Mac. I eventually stumbled across "Android Studio's -> Device File Explorer" and could see all my created files and folders with it so realised the issue is with AFT.
I now make use of Airdroid to browse and download device files.
applicable reference
https://docs.nativescript.org/angular/ng-framework-modules/file-system
(angular docs but relevant to nativescript-vue as well)
Do you know your external(sd card) path?
If it is like /storage/emulated/0, then you could try this to create a folder or file.
import * as fs from "tns-core-modules/file-system";
let externalPath= fs.path.join(android.os.Environment.getExternalStorageDirectory().getAbsolutePath().toString());
//Create a folder with known path
var folder: fs.Folder = fs.Folder.fromPath(sdCardPath+"/test");
//Create a file
var testFile: fs.File = folder.getFile("test.txt");
console.log("Path " + folder.path)
User should be able to access this fold and file. It is in device internal storage which is "external" folder.
I still try to figure out how to get access to sd card but hope above code work for you.
I have the same issue and finally solved it by adding android:requestLegacyExternalStorage="true" inside the AndroidManifest.xml file
follow the thread here
I would like to save some files in different directories under Android/data/data/my_app_package/files directory. I am able to obtain the path to the "files" directory by calling context.getExternalFilesDir(null), but can't create a directory under the path returned. Does anyone know why?
Thanks,
File root = SoundApplication.getAppContext().getExternalFilesDir(null);
File soundDir = new File(root, "/sound-dir");
if (!soundDir.exists()) {
boolean dir = soundDir.mkdirs();
System.out.println("dir = " + dir);
}
soundDir.mkdirs() returned true, but I don't see "sound-dir" in Android.data.mayPackage.files directory with Windows explorer.
I am trying to read from a .txt file located within the project structure. After the app has been compiled to the device (tested on both Android and iOS), I begin by checking if the file exists. It does not seem to.
fileAccess.ts:
import fs = require("file-system");
export class FileAccess
{
public data(filePath: string)
{
let exists = fs.File.exists(filePath);
console.log(exists);
}
}
test.txt (located in same directory as fileAccess.ts):
1;DAC
Calling data("./test.txt"); on an instance of FileAccess, the console prints false.
I assume that either I am referencing the file wrong, or the file is not being copied to the device. But which is it, and how do I fix it?
You could use knownFolders for that. Assuming test.txt is in the root of the app folder:
let appPath = fs.knownFolders.currentApp().path;
let myTextFile = appPath + "/test.txt"
I'm currently trying to natively call on an executable on the shell from my Android Project which is in my application home directory. I can see via the command line when using ADB that my file is not getting executable file permissions, however. I've tried the following:
File file = new File(myFileLocation);
if(file.exists()){
boolean executable = file.setExecutable(true);
}
'executable' remains false.
I've also tried the following:
Process processChmod = Runtime.getRuntime().exec("/system/bin/chmod u+x " + myExecutableLocation);
processChmod.waitFor();
When I try to issue a command on the process, I get the following IOException:
java.io.IOException: Permission denied java.io.IOException: Error
running exec(). Command: [/storage/emulated/0/myApp/iperf, -c,
iperf.eltel.net] Working Directory: null Environment: null
The command I'm trying to issue is:
Runtime.getRuntime().exec("/storage/emulated/0/myApp/iperf -c iperf.eltel.net")
Again, at this stage, I can see that the file permissions on the process I wish to use is simply r/w and not executable. Any help appreciated! By the way, I'm trying to use the 'iPerf' C library and there is a compiled armeabi module as well as the original sources/JNI code. There are no compilation issues and this looks like a file permissions issue more than anything.
Files located on SD card aren't executable. Move the file to internal storage (e.g. to /data/local or, if you get permission denied, to data/local/tmp).
That is how I did it :
String filePath = getFilesDir().getAbsolutePath() + File.separator + "myFileName";
File myFile = new File(filePath);
if (!myFile.canExecute()) {
Log.d("File is not executable, trying to make it executable ...");
if (myFile .setExecutable(true)) {
Log.d("File is executable");
} else {
Log.d("Failed to make the File executable");
}
} else {
Log.d("File is executable");
}
I think Android's chmod does not understand u+x syntax, try using the numeric notation like 744.