Good day,

I myself am never experienced in java, I think you will educate me better. There is a code for android 2.1, I want to save certain data (short text files) from one function. It is highly desirable to save not in internal, but on a USB flash drive. The easiest way of course is to pour into the root, but I am an adherent of tearing hands off for that. Therefore, the question is how to determine where to write?

I tend to determine the path in this way (but strains hard coding :))

private String appPrivateDir = Environment.getDataDirectory().toString()+"/data/"+getClass().getPackage().getName().toString()+"/files"; 

still thinking about this

 Environment.getExternalStorageDirectory().getPath().toString(); 

but it succinctly displays only / sdcard, which is also not very comfortable))

ps and the second abstract question, with the help of sharedPreferences can multiline values ​​be saved? getString () will not be enough ..

    2 answers 2

    Everything is much simpler:

     File previewDir = new File(getExternalFilesDir( Environment.DIRECTORY_DOWNLOADS).getAbsolutePath()); 

    As a result, when performing

     previewDir.mkdir(); 

    A hierarchy of the form will be created on the SD card: /Android/data/your_package_name/files/downloads/ , where your_package_name is the name of the application package specified in the manifest. And further, you can already put the files here. There are other constants in the Environment class. And there was an article on Habré about it. Look, it will be useful.

    I’ll add:

    1. This is the recommended way to store files (in the / Android folder and beyond)
    2. When you delete an application, this data is also deleted, which is very important. The user will not need to constantly use the pens to delete data from the card and shout obscenities at your application

    UPD2

    On the SharedPreferences account. What prevents to save the line with the characters of the new line translation - "\ n"? Try it, it should work. In any case, SharedPreferences is just an XML file inside the application directory.

    • Environment.DIRECTORY_DOWNLOADS is first available in API 2.2 - Anton Shevtsov
    • Do in the manifest or in the project property - targetSDK = 2.3.3, minSDK = 2.1 and everything will be fine with you. What is the problem? Now most of the devices under version 2.3.x. And you declare about 2.2. - DroidAlex
    • I want to see what the path will be, but it will fall into exeption .. What am I doing wrong? String previewDir = getExternalFilesDir (Environment.DIRECTORY_DOWNLOADS) .getAbsolutePath (); Toast toast = Toast.makeText (getApplicationContext (), previewDir, Toast.LENGTH_LONG); toast.setGravity (Gravity.CENTER, 0, 0); toast.show (); - Anton Shevtsov
    • I have a suspicion that due to the output of getApplicationContext (). You should not call him without knowing why. The context, in simple terms, is the current activity, so it’s more correct to write like this: Toast.makeText (CurrentActivity.this, previewDir, Toast.LENGTH_LONG); In general, to get an answer, write more and what epepshn falls. - DroidAlex
    • 04-25 16: 40: 52.581: E / AndroidRuntime (280): java.lang.IllegalStateException: Could not execute method of the activity And it works .. String previewDir = "Hello"; Toast toast = Toast.makeText (getApplicationContext (), previewDir, Toast.LENGTH_LONG); toast.setGravity (Gravity.CENTER, 0, 0); toast.show (); - Anton Shevtsov

    I solve it like this:

    I redefine the Context.getExternalFilesDir() method in my Context.getExternalFilesDir() without overriding it will still refer to Environment.getExternalFilesDir() ).

    After redefining, it looks at me for something like / sdcard / mydir - naturally, during redefinition, you can arrange to check the presence of the directory, whether the card is mounted and so on.

    • And why do this? Bike pure water is obtained. Use ready-made methods for ready-made functionality. And besides, you lose the convenience of cleaning when you uninstall the application. - DroidAlex