Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio: context.getFilesDir() returns a path [/data/user/0/com.example.filesexperimenting/files/] that I can not find. What am I missing?

Tags:

android

I am trying to use Android's internal helpers to get a path from the system for my file first and then put my files where the system wants. Because tomorrow they might change their minds.

I made a simple program to explore this subject. Here is my code;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String path = letsMakeAfile(this, "myFile.txt");

    }

    private static String letsMakeAfile(Context context, String nameOfFile) {

        String strOfFinalPath ="";

        //ask the system what path to use...
        String strOfContextPath = context.getFilesDir() + "/";

                //line above doesnt work without ' + "/" ' or something on the end
                //line above records this path:  /data/user/0/com.example.filesexperimenting/files/
                        //this appears to be an invalid path unless "user" is a hidden directory

        Log.d("IDIOT", "strOfContextPath: "+ strOfContextPath);

        try
        {
            File file = new File(strOfContextPath, nameOfFile);

            if (file.exists() == false) {
                file.mkdirs();
                //after this line "makes dirs" is file automatically still made and dropped in?

                letsMakeAfile(context, nameOfFile);
                //I assume not so Ive made a recursive call


            }

            else
                ;
                //escape recursion....

            strOfFinalPath = file.getAbsolutePath();
            //Here I record the path where I hope the file is located

            Log.d("IDIOT", "strOfFinalPath: "+ strOfFinalPath);

        }

        catch (Exception e) {
            e.printStackTrace();
            Log.d("IDIOT", "CATCH ERROR: "+ e.getLocalizedMessage());
        }

        //runs without a catch

        return strOfFinalPath;
    }
}

Logcat:

   2019-04-09 09:59:22.901 16819-16819/? D/IDIOT: strOfContextPath: /data/user/0/com.example.filesexperimenting/files/
   2019-04-09 09:59:22.901 16819-16819/? D/IDIOT: strOfFinalPath: /data/user/0/com.example.filesexperimenting/files

Ultimately I am getting a path of /data/user/0/com.example.filesexperimenting/files/ from context.getFilesDir() which appears to be an invalid path unless "user" is a hidden directory (then why can I see root?). In Device File Explorer under data the only other directories are app, data and local

enter image description here

What am I missing? I'll assume its something with file.makedirs()

Full disclosure, I am a student and there is not a lot out there on this so your replies, while obvious to you at your experience level, should help others. I have some experience with Java and more with C++ but Android is new to me. Thanks in advance!

like image 927
spencemw Avatar asked Apr 09 '19 16:04

spencemw


People also ask

What is context getfilesdir in Android?

Android Context getFilesDir () Returns the absolute path to the directory on the filesystem where files created with #openFileOutput are stored. Returns the absolute path to the directory on the filesystem where files created with #openFileOutput are stored.

What does the method getfilesdir() return in Java?

The method getFilesDir () returns The path of the directory holding application files. The following code shows how to use Java Context getFilesDir ()

Is it safe to use the file path getfilesdir?

Thus if you're just trying to dig through your filesystem and see a file, it's safe to use this path, but if you're using this path for some functionality across multiple platform versions, you should only reference it using getFilesDir (). Show activity on this post. What are you planning on using this file for?

What permissions are required for getfilesdir() to work?

No additional permissions are required for the calling app to read or write files under the returned path. The method getFilesDir () returns The path of the directory holding application files. The following code shows how to use Java Context getFilesDir ()


2 Answers

So, in talking outside of StackExchange it appears that using java.io like I am trying to in the example can cause some problems because of the preset file directories that may be locked or restricted that Java io might not know about.

Android has it's own method openFileOutput(String name, int mode) that has the ability to create the app resource file and directory it belongs in.

Description copied from class: android.content.Context

Actions:
~Open a private file associated with this Context's application package for writing.
~Creates the file if it doesn't already exist.
~No additional permissions are required for the calling app to read or write the returned file.

Params:
~name – The name of the file to open; can not contain path separators.
~mode – Operating mode.

Returns: The resulting FileOutputStream.

Throws: java.io.FileNotFoundException

like image 119
spencemw Avatar answered Oct 21 '22 18:10

spencemw


If you want to be able to navigate to the location of your saved files through the file explorer (either in Android Studio or the Files app on the phone) you should use Context.getExternalFilesDir().

Context.getFilesDir() returns a directory not accessible by anyone BUT the creating application. So if you would like to see what is in this file you would need to open it with the same application that wrote it. IE: Print the contents to the screen after you save it in your app.

Context.getExternalFilesDir() returns a directory completely accessible by anyone and any application. So files created and saved in this external directory can be seen by Android Studio's file explorer as the OP has screenshot or by any application installed on the phone.

What is nice about both of these methods is that as long as you are only accessing files you have created you never need to ask the user for storage permissions Read or Write. If you would like to write to someone else's external files dir then you do.

Source

like image 36
J Blaz Avatar answered Oct 21 '22 18:10

J Blaz