Android,如何选择保存文件的位置?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11848224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android, how to choose save file location?
提问by David
is there any solution how to choose the saving files location? maybe with the original file browser, to choose the destination?
有没有办法选择保存文件的位置?也许使用原始文件浏览器,选择目的地?
thank you!
谢谢你!
采纳答案by Basbous
All you need is Android Directory Picker
您只需要Android 目录选择器
回答by Maxim
Better to save files with your app namespace:
最好使用您的应用程序命名空间保存文件:
String extStorage = Environment.getExternalStorageState();
path = extStorage+"/Android/data/com.mydomain.myapp/";
It will be deleted when app gets uninstalled.
当应用程序被卸载时,它将被删除。
Here is actually everything about data storing.
http://developer.android.com/guide/topics/data/data-storage.html
这实际上是有关数据存储的所有内容。
http://developer.android.com/guide/topics/data/data-storage.html
From the above link:
从上面的链接:
If you're using API Level 7 or lower, use
getExternalStorageDirectory()
, to open aFile
representing the root of the external storage. You should then write your data in the following directory:/Android/data/<package_name>/files/
如果您使用 API 级别 7 或更低级别,请使用
getExternalStorageDirectory()
, 打开File
代表外部存储根的 。然后,您应该将数据写入以下目录:/Android/data/<package_name>/files/
回答by Pork 'n' Bunny
It's probably easiest and expected to save this out to the dedicated area on the external SD card.
将其保存到外部 SD 卡上的专用区域可能是最简单的,也是预期的。
You can get this folder by calling
您可以通过调用获取此文件夹
file://localhost/Applications/android-sdk-macosx/docs/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)
file://localhost/Applications/android-sdk-macosx/docs/reference/android/os/Environment.html#getExternalStoragePublicDirectory(java.lang.String)
File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES)
You can also append a sub-folder to reference the source better ie. your app.
您还可以附加一个子文件夹以更好地引用源,即。你的应用程序。
You can either choose DIRECTORY_PICTURES or DIRECTORY_MOVIES. If it's user created I'd probably stick to pictures.
您可以选择 DIRECTORY_PICTURES 或 DIRECTORY_MOVIES。如果它是用户创建的,我可能会坚持使用图片。
To be helpful you can also call the media scanner to add it to the appropriate content provider system wide.
为了有所帮助,您还可以调用媒体扫描器将其添加到适当的内容提供商系统范围内。
sendBroadcast( new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory()))
);
If you MUST get a file chooser going, which isn't the recomened approach to expose the user to the file system. Try the file chooser form Open Intents. http://www.openintents.org/en/node/164
如果您必须使用文件选择器,这不是将用户暴露给文件系统的推荐方法。尝试文件选择器表单 Open Intents。http://www.openintents.org/en/node/164