1

对不起我的英语,但我想写在这个文件中,因为在我看来是最好的。现在我的问题:我想在内部存储中创建一个文件夹以与 2 个应用程序共享。在我的应用程序中,我从服务器下载了一个 Apk 并运行它。在我使用外部存储之前,一切正常。现在我想为没有外部存储的用户使用内部存储。

我用这个:

String folderPath = getFilesDir() + "Dir"

但是当我尝试运行 Apk 时,它不起作用,而且我在手机上找不到这个文件夹。

谢谢..

4

4 回答 4

4

这篇文章:

正确方法:

  1. 为您想要的目录创建一个文件(例如,File path=new
  2. 文件(getFilesDir(),“我的文件夹”);)
  3. 如果该文件不存在,则对该文件调用 mkdirs() 以创建该目录
  4. 为输出文件创建一个文件(例如,File mypath=new File(path,"myfile.txt");)
  5. 使用标准 Java I/O 写入该文件(例如,使用 new BufferedWriter(new FileWriter(mypath)))

享受。

还要创建我使用的公共文件:

    /**
 * 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());
    }
}

只需将模式设置为 MODE_WORLD_WRITEABLE 或 MODE_WORLD_READABLE(请注意,它们已从 api lvl 17 弃用)。

您也可以使用theContext.getDir();但请注意文档所说的内容:

检索,如果需要,创建一个新目录,应用程序可以在其中放置自己的自定义数据文件。您可以使用返回的 File 对象在此目录中创建和访问文件。请注意,通过 File 对象创建的文件只能由您自己的应用程序访问;您只能设置整个目录的模式,而不是单个文件的模式。

最良好的祝愿。

于 2013-06-24T16:18:13.177 回答
2

您可以在现有的系统公共文件夹中创建一个公共文件夹,可以从内部存储访问一些公共文件夹:

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";

要创建您的文件夹,请使用以下代码:

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

在此示例中,将在 Documents 中创建一个公共,并且可以在任何文件的 Android 资源管理器应用程序中可见。

于 2017-09-01T16:03:39.523 回答
-1

试试下面

File mydir = context.getDir("Newfolder", Context.MODE_PRIVATE); //Creating an internal dir;
if(!mydir.exists)
{
     mydir.mkdirs();
}     
于 2013-06-24T16:19:57.363 回答
-1

这是我用过的,对我来说很好用:

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

如果不存在,这将创建一个新目录,如果已经存在,则使用现有目录。

于 2013-06-24T16:21:15.603 回答