I have a rooted device running MarshMallow. I have integrated Chainfire's library Library in my code. I get the superuser prompt and granted the permission. After that I am trying to get the folders inside /data/misc/ but it returns me null. I have READ_EXTERNAL_STORAGE permission also in manifest and in App->Permissions have granted the same.
What I have tried so far ..
Checked /data/misc/ folder in ES File Explorer. It does have folders.
From cmd, ran
adb shell
su
cd /data/misc/
ls
Was able to see the folders in cmd.
Can anyone please help how to solve this?
Code :
private final static String ROOT_DIR = "/data/misc/";
#Override
protected Void doInBackground(Void... params) {
// Let's do some SU stuff
suAvailable = Shell.SU.available();
if (suAvailable) {
Log.d("TAG", "SU there");
//List<String> output = Shell.SU.run("mount -o rw,remount /");
//Log.d("TAG","Output ="+output); // empty
File file = new File(ROOT_DIR);
if (file.exists()) {
Log.d("TAG", "File exists"+ file.getAbsolutePath()+ file.isDirectory());
File file1[] = file.listFiles(); // RETURNS NULL HERE
}
}
}
Related
I am intermittently getting Too many open files exceptions when I try to write to a text file in my Xamarin / Android app.
I understand that this is because each Android app has a limit on the number of files it can have open, and obviously my app is exceeding it in some circumstances. Hence I must be leaving some files open / not disposing of them properly somewhere.
I would like to know if I can programatically list the files that my app has open at any given time? Obviously the operating system knows this, is it possible to do this from within my app? This will help me find the problem.
Thanks to fiddler, this is how I solved it in case anyone else needs to do it in the future (in C# / Xamarin):
private static List<string> m_commandData;
private static void GetOpenedFiles()
{
m_commandData = new List<string>();
RunCommand("lsof");
RunCommand("ps | grep TestNoFilesOpen");
}
private static void RunCommand(string command)
{
string data;
using (Java.Lang.Process process = Java.Lang.Runtime.GetRuntime().Exec(command))
{
Stream stream = process.InputStream;
StreamReader bodyReader = new StreamReader(stream);
data = bodyReader.ReadToEnd();
}
m_commandData.Add(data);
}
private static void WriteCommands()
{
for (int i = 0; i < m_commandData.Count; i++)
{
using (TextWriter tw = new StreamWriter(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/NoFilesOpen/adb_" + i.ToString() + ".txt"))
{
tw.Write(m_commandData[i]);
tw.Close();
}
}
m_commandData = new List<string>();
}
You could use the Unix lsof command in the ADB shell.
1) Start the shell:
$ adb shell
2) Find the process ID of your app:
shell#android:/ $ ps | grep <packagename>
3) List files opened by your app:
shell#android:/ $ lsof <pid>
I want to test android apps for accessibility violation. Is it possible to use uiautomator api and monkeyrunner api to capture the layout of all screens?
You can simply use AndroidViewClient's dump:
$ dump --help
usage: dump [OPTION]... [serialno]
Options:
-H, --help prints this help
-V, --verbose verbose comments
-v, --version
-I, --ignore-secure-device ignore secure device
-E, --ignore-version-check ignores ADB version check
-F, --force-view-server-use force view server use (even if UiAutomator present)
-S, --do-not-start-view-server don't start ViewServer
-k, --do-not-ignore-uiautomator-killed don't ignore UiAutomator killed
-w, --window=WINDOW dump WINDOW content (default: -1, all windows)
-i, --uniqueId dump View unique IDs
-x, --position dump View positions
-d, --content-description dump View content descriptions
-c, --center dump View centers
-f, --save-screenshot=FILE save screenshot to file
-W, --save-view-screenshots=DIR save View screenshots to files in directory
-D, --do-not-dump-views don't dump views, only useful if you specified -f or -W
In you case, probably
$ dump -d
is enough.
EDIT
If you want something more complex than just saving the logical content of the screen, you can use culebra (another tool in AndroidViewClient) that allows you to generate automated tests using a GUI:
$ culebra -UG -o mytest.py
You can interact with the mirror representation of the device screen in culebra's main window to generate the test. Save it. Run it on any device, even a completely different one.
import android.util.Log;
import com.android.uiautomator.testrunner.UiAutomatorTestCase;
import java.io.File;
public class TestUtils {
private UiAutomatorTestCase mTestCase;
public TestUtils(UiAutomatorTestCase testCase) {
mTestCase = testCase;
}
public void sleepInSeconds(int timeInSeconds) {
mTestCase.sleep(timeInSeconds * 1000);
}
public String getFileName(String fileNameSuffix) {
String tempFileName = fileNameSuffix.toLowerCase().replace(" ", "_")
.replace("?", "_");
String fileName = String.format("%d_%s", System.currentTimeMillis(),
tempFileName);
return fileName;
}
// Note: You need to have file name as a single string.
// This will get the UiHierarchy
public String saveUiHierarchyToFile(String nameOfFile) {
String fileName = nameOfFile;
if (nameOfFile.contains(" ")) {
fileName = getFileName(nameOfFile);
}
sleepInSeconds(10);
try {
File hierarchyFile = new File(fileName + ".uix");
mTestCase.getUiDevice().dumpWindowHierarchy(hierarchyFile
.getAbsolutePath());
Log.d(mTestCase.getClass().getSimpleName(), String.format("\t ** UI hierarchy: " +
"/data/local/tmp%s", hierarchyFile.getAbsolutePath()));
return hierarchyFile.getAbsolutePath();
} catch (Exception e) {
Log.d(mTestCase.getName(), "\t ** Unable to save view hierarchy" +
" to a file", e);
}
return null;
}
// This will get the screen shot
public String saveScreenshot(String nameOfFile) throws Exception {
String fileName = nameOfFile;
if (nameOfFile.contains(" ")) {
fileName = getFileName(nameOfFile);
}
sleepInSeconds(10);
File screenshotFile = new File("/data/local/tmp",
fileName + ".png");
mTestCase.getUiDevice().takeScreenshot(
new File(screenshotFile.getAbsolutePath()));
Log.d(mTestCase.getClass().getSimpleName(), String.format("\t ** Screenshot: %s",
screenshotFile.getAbsolutePath()));
return screenshotFile.getAbsolutePath();
}
// Store UiAutomator viewer
public void storeUiAutomatorViewerFiles(String nameOfFile)
throws Exception {
String fileName = getFileName(nameOfFile);
saveScreenshot(fileName);
saveUiHierarchyToFile(fileName);
}
}
I am developing a root app for modifying build.prop programatically.
I copied build.prop from /system/build.prop to /sdcard/
but after modifying it I was unable to copy back to root partition
here is my code for copying build.prop from /system/build.prop to /sdcard/
protected void SUCommand()
{
String sSUCommand = "cp /system/build.prop /sdcard/";
final String[] sCommand = {"su","-c",sSUCommand};
Thread thSUProcess = new Thread()
{
public void run()
{
try
{
Process p = Runtime.getRuntime().exec(sCommand);
}
catch(Exception e){}
}
};
thSUProcess.start();
}
I changed String sSUCommand = "cp /system/build.prop /sdcard/"; to String sSUCommand = "cp /sdcard/build.prop /system/"; to copy it to system partition
but didn't worked
I already rooted my phone and running lot of root apps successfully
please tell me the right way to do it
You might want to mount system partition as rw instead of rd so as to do it
https://android.stackexchange.com/questions/25250/how-to-mount-system-in-rw-mode-if-no-custom-recovery
I have an extension file under "/sdcard/Android/obb/com.example.obbtest/vid-exp1.obb". It contains an MP4 file and I want to mount the .obb to read the file.
This is what I'm doing to mount it:
String obbDir = "/sdcard/Android/obb/com.example.obbtest/vid-exp1.obb";
.
StorageManager storage = (StorageManager) getApplicationContext().getSystemService(STORAGE_SERVICE);
storage.mountObb(obbDir, null, listener);
This is the listener code:
OnObbStateChangeListener listener = new OnObbStateChangeListener() {
#Override
public void onObbStateChange(String path, int state) {
if (state == OnObbStateChangeListener.MOUNTED) {
toastString("Mounted! According to the listener");
//Test it with the isObbMounted()
if (storage.isObbMounted(obbDir)) {
toastString("Efectively mounted!");
} else {
toastString("Not really :(");
}
toastString(storage.getMountedObbPath(obbDir));
} else {
tuestameString("NOT mounted according to the listener");
}
}
};
Unfortunately the output I get is a toast saying "Mounted! According to the listener" followed by "Not really :(". I designed this test because when I tried getMountedObbPath(obbDir) I got a null String instead of the path. I've made sure the .obb file exists and all that, without it or without the correct encription key I don'get "Mounted!...".
I don't understand why OnObbStateChangeListener.MOUNTED is true but isObbMounted(obbDir) false. Does anyone know what I doing wrong?
Had this problem on a Samsung device. It happens when /mnt/sdcard/ is not a directory but a symlink to another dir (in my case that was /storage/sdcard0).
In this case StorageManager uses not the path to the obb that you have specified, but the path with symlinks resolved: isObbMounted("/mnt/sdcard/my.obb") returns false and isObbMounted("/storage/sdcard0/my.obb") is true.
To access the mounted obb, you must not use the path obbDir, but the path passed to onObbStateChange() in the first argument: isObbMounted(path).
I need to remove a particular folder from the /mnt/sdcard/new.
I am looking at the folder with the DDMS in Eclipse.
How do we remove a particular folder.
Thanks in advance.
You can use rm command with -r parameter to delete a nonempty folder.
C:\> adb shell
$ rm -r /mnt/sdcard/Android/data/mydirectory/
NOTE: rmdir can delete only a nonempty folder.
C:\>adb shell
$ rmdir /mnt/sdcard/Android/data/mydirectory/
Please use below method for delete folder from sdcard
// Deletes all files and subdirectories under dir.
// Returns true if all deletions were successful.
// If a deletion fails, the method stops attempting to delete and returns false.
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i=0; i<children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}
write below code for call deleteDir() method
// Delete an empty directory
boolean success = (new File("directorypath")).delete();
if (!success) {
// Deletion failed Message
}
boolean success = (
new File("/data/data/yourpackege/New Folder")).delete();
if (!success) {
// Deletion failed Message
Toast.makeText(getApplicationContext(),"not deleted : ", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getApplicationContext()," deleted : ", Toast.LENGTH_LONG).show();
}
If you want to delete any folder from ddms first you have to go to adb shell through cmd
simple go to the path where your sdk\platform-tools\ is located ,there is your adb shell
to run command for deleting folders you must first root your device by simply typing
adb root
then you can delete the folder using
rmdir /mnt/sdcard/folder
to delete folder with files
rm -r /mnt/sdcard/folder
hope my answer will help anyone(beginners)