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

java-在内部存储中创建公用文件夹,第1张

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

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

我用这个:

String folderPath = getfilesDir() + "Dir"

但是当我尝试运行Apk时,它不起作用,并且在手机上找不到该文件夹​​.

谢谢..

解决方法:

从this帖子:

Correct way:

Create a file for your desired directory (e.g., file path=new file(getfilesDir(),”myfolder”);)Call mkdirs() on that file to create the directory if it does not existCreate a file for the output file (e.g., file mypath=new file(path,”myfile.txt”);)Use standard Java I/O to write to that file (e.g., using 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设置为MODE_WORLD_WRITEABLE或MODE_WORLD_READABLE(请注意,它们已从API lvl 17中弃用).

您也可以使用Context.getDir();但请注意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. 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.

最好的祝愿.

总结

以上是内存溢出为你收集整理的java-在内部存储中创建公用文件夹全部内容,希望文章能够帮你解决java-在内部存储中创建公用文件夹所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093768.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存