创建内部存储公用文件夹 [英] Create a public folder in internal storage

查看:161
本文介绍了创建内部存储公用文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我用这样的:

 字符串FOLDERPATH = getFilesDir()+目录

但是当我尝试运行APK,不能正常工作。我无法找到我的手机上该文件夹。

感谢您..


解决方案

从<一个href=\"http://stackoverflow.com/questions/5766609/save-internal-file-in-my-own-internal-folder-in-android\">this岗位:


  

正确方法:


  
  

      
  1. 创建一个文件所需目录(例如,文件路径=新

  2.   
  3. 文件(getFilesDir(),MyFolder文件);)

  4.   
  5. 呼叫mkdirs()在该文件上创建目录,如果它不存在,

  6.   
  7. 创建输出文件一个文件(例如,文件mypath中=新的文件(路径,myfile.txt的);)

  8.   
  9. 使用标准的Java I / O写入该文件(例如,使用新的BufferedWriter(新的FileWriter(mypath中)))

  10.   

享受。

另外要创建公共文件我使用:

  / **
 * Context.MODE_PRIVATE将创建文件(或替换同名的文件),并使其专用于您的应用程序。
 *用其他的模式有:MODE_APPEND,MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE。
 * /公共静态无效createInternalFile(上下文theContext,字符串theFileName,字节[]海图,INT theMode)
{
    FOS的FileOutputStream = NULL;    尝试{
        FOS = theContext.openFileOutput(theFileName,theMode);
        fos.write(海图);
        fos.close();
    }赶上(FileNotFoundException异常五){
        Log.e(TAG[createInternalFile]+ e.getMessage());
    }赶上(IOException异常五){
        Log.e(TAG[createInternalFile]+ e.getMessage());
    }
}

刚刚成立theMode为MODE_WORLD_WRITEABLE或MODE_WORLD_READABLE(注意他们是来自API pcated拉特17日$ P $)。

您也可以使用 theContext.getDir(); 但需要注意什么医生说:


  

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


最良好的祝愿。

sorry for my english, but i want to write in this forum 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 download 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, 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 (e.g., 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 (e.g., File mypath=new File(path,"myfile.txt");)
  5. Use standard Java I/O to write to that File (e.g., 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.

这篇关于创建内部存储公用文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆