Private folder on internal / external storage on Android

I want to create a personal folder for my application data. This folder should be deleted automatically when the application is deleted from the device. According to http://developer.android.com/training/basics/data-storage/files.html , I need to create a personal folder. How can i achieve this. My code is below. The folder is not deleted when the application is uninstalled.

 public static String getAppFilePath(Context context) {

        String path = "";
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            path = Environment.getExternalStorageDirectory()
                    .getAbsolutePath();

            path += "/myFolder";

        } else {

            ContextWrapper c = new ContextWrapper(context);
            path = c.getFilesDir().getPath();
        }
        File ret = new File(path);
        if (!ret.exists()) {
            ret.mkdirs();
        }
        return path;
    }
+4
source share
2 answers

you are doing it wrong.

from the documentation you specified:

private , , getExternalFilesDir() , . , , , .

, :

public File getAlbumStorageDir(Context context, String albumName) {
    // Get the directory for the app private pictures directory. 
    File file = new File(context.getExternalFilesDir(
            Environment.DIRECTORY_PICTURES), albumName);
    if (!file.mkdirs()) {
        Log.e(LOG_TAG, "Directory not created");
    }
    return file;
}

, getExternalFilesDir() null. .

, getExternalFilesDir() , , .. , , , , , , - getExternalStoragePublicDirectory().

getExternalFilesDir , .


getExternalFilesDir()

type . null : DIRECTORY_MUSIC, DIRECTORY_PODCASTS, DIRECTORY_RINGTONES, DIRECTORY_ALARMS, DIRECTORY_NOTIFICATIONS, DIRECTORY_PICTURES DIRECTORY_MOVIES.

, , null .

+5

. , .

-1

Source: https://habr.com/ru/post/1612383/


All Articles