简体   繁体   中英

Create a public folder in internal storage

Sorry for my English, but I want to write in this file because in my opinion is the best. Now my problem: I want to create a folder in Internal storage to share with 2 application. In my app, I downloaded an Apk from my server and I run it. Before I used external storage and everything worked. Now I want to use the internal storage for users that don't have an external storage.

I use this:

String folderPath = getFilesDir() + "Dir"

but when i try to run the Apk, it doesn't work, and I can't find this folder on my phone.

Thank you..

From this post :

Correct way:

  1. Create a File for your desired directory (eg, File path=new
  2. File(getFilesDir(),"myfolder");)
  3. Call mkdirs() on that File to create the directory if it does not exist
  4. Create a File for the output file (eg, File mypath=new File(path,"myfile.txt");)
  5. Use standard Java I/O to write to that File (eg, using new BufferedWriter(new FileWriter(mypath)))

Enjoy.

Also to create public file I use :

    /**
 * Context.MODE_PRIVATE will create the file (or replace a file of the same name) and make it private to your application.
 * Other modes available are: MODE_APPEND, MODE_WORLD_READABLE, and MODE_WORLD_WRITEABLE.
 */

public static void createInternalFile(Context theContext, String theFileName, byte[] theData, int theMode)
{
    FileOutputStream fos = null;

    try {
        fos = theContext.openFileOutput(theFileName, theMode);
        fos.write(theData);
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e(TAG, "[createInternalFile]" + e.getMessage());
    } catch (IOException e) {
        Log.e(TAG, "[createInternalFile]" + e.getMessage());
    }
}

Just set theMode to MODE_WORLD_WRITEABLE or MODE_WORLD_READABLE (note they are deprecated from api lvl 17).

You can also use theContext.getDir(); but note what doc says :

Retrieve, creating if needed, a new directory in which the application can place its own custom data files. You can use the returned File object to create and access files in this directory. Note that files created through a File object will only be accessible by your own application; you can only set the mode of the entire directory, not of individual files.

Best wishes.

You can create a public into a existing system public folder, there is some public folder accessible from internal storage :

public static String DIRECTORY_MUSIC = "Music";
public static String DIRECTORY_PODCASTS = "Podcasts";
public static String DIRECTORY_RINGTONES = "Ringtones";
public static String DIRECTORY_ALARMS = "Alarms";
public static String DIRECTORY_NOTIFICATIONS = "Notifications";
public static String DIRECTORY_PICTURES = "Pictures";
public static String DIRECTORY_MOVIES = "Movies";
public static String DIRECTORY_DOWNLOADS = "Download";
public static String DIRECTORY_DCIM = "DCIM";
public static String DIRECTORY_DOCUMENTS = "Documents";

To create your folder, use this code :

File myDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS), "MyPublicFolder");
myDirectory.mkdir();

With this example, a public will be created in Documents and can be visible in any file's explorer app for Android.

try the below

File mydir = context.getDir("Newfolder", Context.MODE_PRIVATE); //Creating an internal dir;
if(!mydir.exists)
{
     mydir.mkdirs();
}     

This is what i have used and is working fine for me:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
File file = new File(extStorageDirectory, fileName); 
    File parent=file.getParentFile();
            if(!parent.exists()){
                parent.mkdirs();
            }

This will create a new directory if not already present or use the existing if already present.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM