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

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

问题描述

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



我使用这个:

  String folderPath = getFilesDir()+Dir

但是当我尝试运行apk时,不行。我无法在手机上找到这个文件夹。



谢谢..

解决方案

帖子:


正确的方式:


  1. 创建文件路径=新

  2. 文件(getFilesDir(),myfolder);)

  3. 调用mkdirs( )在该文件上创建目录(如果不存在)

  4. 为输出文件创建文件(例如,File mypath = new File(path,myfile.txt);)

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


享受。



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

  / ** 
* Context.MODE_PRIVATE将创建文件(或替换同名文件)和mak它对您的应用程序是私有的。
*其他可用的模式有:MODE_APPEND,MODE_WORLD_READABLE和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(注意它们是



您还可以使用 theContext.getDir(); ,但请注意什么文件说:


检索,如果需要创建一个新的目录,应用程序可以在该目录中放置自己的自定义数据文件。您可以使用返回的File对象来创建和访问此目录中的文件。请注意,通过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天全站免登陆