简体   繁体   English

在内部存储中创建公用文件夹

[英]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.现在我的问题:我想在内部存储中创建一个文件夹以与 2 个应用程序共享。 In my app, I downloaded an Apk from my server and I run it.在我的应用程序中,我从我的服务器下载了一个 Apk 并运行它。 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.但是当我尝试运行 Apk 时,它不起作用,而且我在手机上找不到这个文件夹。

Thank you..谢谢..

From this post :这篇文章:

Correct way:正确做法:

  1. Create a File for your desired directory (eg, File path=new为您想要的目录创建一个文件(例如,文件路径=新
  2. File(getFilesDir(),"myfolder");)文件(getFilesDir(),“我的文件夹”);)
  3. Call mkdirs() on that File to create the directory if it does not exist如果该文件不存在,则对该文件调用 mkdirs() 以创建该目录
  4. Create a File for the output file (eg, File mypath=new File(path,"myfile.txt");)为输出文件创建一个文件(例如,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)))使用标准 Java I/O 写入该文件(例如,使用 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).只需将模式设置为 MODE_WORLD_WRITEABLE 或 MODE_WORLD_READABLE(请注意,它们已从 api lvl 17 开始弃用)。

You can also use theContext.getDir();您也可以使用theContext.getDir(); but note what doc says :但请注意 doc 说什么:

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.您可以使用返回的 File 对象在此目录中创建和访问文件。 Note that files created through a File object will only be accessible by your own application;请注意,通过 File 对象创建的文件只能由您自己的应用程序访问; 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.在此示例中,将在 Documents 中创建一个 public 并且可以在任何文件的 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.这将创建一个新目录(如果尚不存在)或使用现有目录(如果已存在)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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