0

I am trying to write a text file to internal storage of my android application. But it is not possible for me to see if the file is generated or not.

My text file is stored in the following path: data/data/"MyApplcationPackageName"/files/MyFile.txt

Permission : drwxrwx-x

I have tried the following things -

1) Using device file explorer:

Device file explorer does not open my application package. it gives following error if I try to open it. Device File Explorer

2) Terminal:

I have also tried opening it using adb in the terminal. But when I try to open files inside my application package it says permission denied. adb terminal

Please let me know how I can open my text file for debugging. Thanks in advance.

public static void StoreDB() {
    if(isExternalStorageWritable() && checkPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        File file = getFinalDir();
        try {
            FileOutputStream fos = new FileOutputStream(file);
            fos.write("something".getBytes());
            fos.close();
            ToastUtil.showToast(Resource.getAppContext(),"File Saved");
        } catch (IOException e) {
            Log.e("StoreDB", "Exception");
            e.printStackTrace();
        }
    }
}

public static boolean isExternalStorageWritable() {
    if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
        Log.d("External storage", "Writable");
        return true;
    }
    Log.d("External storage", "Not Writable");
    return false;
}

public static boolean checkPermission(String Permission){
    int check = ContextCompat.checkSelfPermission(Resource.getAppContext(),Permission);
    boolean Perm = (check == PackageManager.PERMISSION_GRANTED);
    Log.d("Check Permission", "Result: " + Perm);
    return Perm;
}

private static File getFinalDir() {
    return createDirIfNotExist(Environment.getExternalStorageDirectory().getPath() + "/Co_Ordinate.txt/");
}


public static File createDirIfNotExist(String path) {
    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdirs();
    }
    return dir;
}

Now I am trying to put the file in external storage. Above code always gives IO exception.

  • "File" in Java stores both directories and individual files. In the above case, "file = getFinalDir()" is storing a directory. So you're trying to write text to a directory rather than a file. You need to create the individual file in that directory like this "File backupFile = new File(file, fileName);". And then do " FileOutputStream fos = new FileOutputStream(backupFile);". – Gavin Wright Oct 30 '18 at 12:49

2 Answers2

0

You need to root your phone to view those files. Or you can do it on an emulator by using the Device File Explorer.

EDIT: Or just use an unprotected file path. This will create the directory. After that you just need to save the .txt file to that directory.

private static File getFinalDir() {
    return createDirIfNotExist(Environment.getExternalStorageDirectory().getPath() + "/MyAppName/");
}


public static File createDirIfNotExist(String path) {
    File dir = new File(path);
    if (!dir.exists()) {
        dir.mkdirs();
    }
    return dir;
}
Gavin Wright
  • 3,124
  • 3
  • 14
  • 35
  • Hello, I have used the following commands : adb devices, adb root, adb shell, ls, cd data, cd data, cd "MyPackageName", cd files (Here I am getting permission denied). Do I have to do anything else to root the device? – Skadna Naglapur Ramamurthy Oct 30 '18 at 11:11
  • But the files are encrypted. You can't just request root access to them. The phone itself needs to be rooted or you'll never get to them. Do a Google search for "name of your phone + root". – Gavin Wright Oct 30 '18 at 11:13
  • Thank you. But I am not using mobile. I am using a robot and the text file generated iis the value that I get from the sensor. Is it the same way to root any android device ? – Skadna Naglapur Ramamurthy Oct 30 '18 at 11:47
  • It's a robot running Android? You probably won't be able to root that. Why don't you just write the file to Environment.getExternalStorageDirectory().getPath() instead? That's the sdCard directory and it doesn't require root access. – Gavin Wright Oct 30 '18 at 11:51
  • File file = new File(Environment.getExternalStorageDirectory(), "co_ordinate"); I am using this line to create a file in the directory of storage but it is throwing an exception always – Skadna Naglapur Ramamurthy Oct 30 '18 at 12:16
  • I have been trying also to store in external storage. I have posted the code on top. It always throws exception. – Skadna Naglapur Ramamurthy Oct 30 '18 at 12:44
0

You can do this with Emulator. Run your app in emulator and go to Android Device monitor

Choose device, go to File Explorer menu and search your text file in data folder