我想解析/proc/net/tcp/,但它安全吗?
我应该如何打开和读取文件,/proc/而不是害怕,某些其他进程(或操作系统本身)将在同一时间更改它?
在Android 4.4(KitKat)上,谷歌已经限制了对SD卡的访问.
从Android Lollipop(5.0)开始,开发人员可以使用新的API,要求用户确认允许访问特定文件夹,如此Google-Groups帖子所述.
该帖子指导您访问两个网站:
这看起来像是一个内部示例(可能稍后会在API演示中显示),但很难理解发生了什么.
这是新API的官方文档,但它没有详细说明如何使用它.
这是它告诉你的:
如果您确实需要完全访问整个文档子树,请首先启动ACTION_OPEN_DOCUMENT_TREE以允许用户选择目录.然后将生成的getData()传递给fromTreeUri(Context,Uri)以开始使用用户选择的树.
在导航DocumentFile实例树时,您始终可以使用getUri()来获取表示该对象的基础文档的Uri,以便与openInputStream(Uri)等一起使用.
要在运行KITKAT或更早版本的设备上简化代码,可以使用fromFile(File)来模拟DocumentsProvider的行为.
我对新API有几个问题:
我想以编程方式获取设备内部/外部存储空闲内存的大小.我正在使用这段代码:
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long bytesAvailable = (long)stat.getBlockSize() *(long)stat.getBlockCount();
long megAvailable = bytesAvailable / 1048576;
Log.e("","Available MB : "+megAvailable);
File path = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize = stat2.getBlockSize();
long availableBlocks = stat2.getAvailableBlocks();
String format = Formatter.formatFileSize(this, availableBlocks * blockSize);
Log.e("","Format : "+format);
Run Code Online (Sandbox Code Playgroud)
而我得到的结果是:
11-15 10:27:18.844: E/(25822): Available MB : 7572
11-15 10:27:18.844: E/(25822): Format : 869MB
Run Code Online (Sandbox Code Playgroud)
问题是我想获得1,96GB现在的SdCard的免费记忆.如何修复此代码,以便获得免费大小?
Android在如何处理SD卡和存储方面有很多变化:
从API 23(Android 6)开始,事情似乎再次发生变化......
对于API 23,至少有两件新东西与存储相关:
由于没有配备SD卡的Android 6设备,并且因为模拟器本身并不能真正使用SD卡,所以仍然无法知道发生了什么.
SD卡是否可以使用File-API而不是DocumentFile获取访问权限?
如果我想访问所有外部存储路径(包括SD卡),这是否意味着我需要两次请求此权限:一个用于主外部存储,一个用于SD卡?
在手动授予许可之前,SD卡上的文件是否可以以任何方式访问?
假设用户选择使用"Adoptable Storage Devices",这对于检索应用程序文件路径的各种函数意味着什么?例如:getFilesDir,getExternalFilesDir,...?getExternalFilesDirs的oder会因为它而改变吗?
当用户将应用程序从SD卡移动到SD卡时(使用"可采用的存储设备"),应用程序的文件会发生什么变化?SD卡上的应用程序文件怎么样?他们会留下来吗?或者他们会搬到某个地方?
例如,如果应用程序在SD卡上有"file1.txt",则在路径"/ storage/extSdCard/Android/data/appPackageName"上,它有一个文件"file2.txt"(或者甚至是同一个名字)在路径"/ storage/emulated/0/Android/data/appPackageName"上的主外部存储上.切换后,这些文件会发生什么?如果有的话,他们将如何合并到一个文件夹中?
将应用程序移动到SD卡(使用"Adoptable Storage Devices")时,是否意味着不会使用内部存储?
android sd-card android-permissions android-external-storage android-6.0-marshmallow
一开始,用户可以使用新的存储访问框架选择文件(假设应用程序是API> 19):
https://developer.android.com/guide/topics/providers/document-provider.html
然后通过保存URI来保存对所选文件的引用,如下所示:
content://com.android.providers.downloads.documments/document/745
Run Code Online (Sandbox Code Playgroud)
(在这种情况下,文件来自默认下载dir`).
稍后,我想让用户打开这些文件(例如,他们的名字显示在UI列表中,用户选择一个).
我想用Android着名的意图选择器功能做到这一点,我所拥有的只是上面的URI对象......
谢谢,
android android-intent android-file storage-access-framework android-afilechooser
我的应用程序下载并解压缩特定文件夹中的文件:
output = new FileOutputStream(realpath, true);
output.write(buffer, 0, bytesRead);
ZipFile zipFile = new ZipFile(realpath);
Run Code Online (Sandbox Code Playgroud)
随着新推出的ACTION_OPEN_DOCUMENT_TREEIntent,我想让用户选择该文件夹.
当测试我收到的值时onActivityResult,我会得到一个类似Path的路径/tree/primary:mynewfolder,这不是物理真实的路径/sdcard/mynewfolder.
Uri treeUri = data.getData();
String sPath = treeUri.getPath();
Log.v("Path from Tree ", sPath);
Run Code Online (Sandbox Code Playgroud)
我的解压方法需要真正的路径:
ZipFile zipFile = new ZipFile(realpath);
Run Code Online (Sandbox Code Playgroud)
如何/sdcard/mynewfolder从Lollipop(API 21和22)中提供的URI 获得真实路径?
当用户点击谷歌硬盘中的"发送文件"按钮并选择我的应用程序.我想获取该文件的文件路径,然后允许用户将其上传到其他位置.
我为kitkat手机检查了这些类似的SO帖子:从URI,Android KitKat新的存储访问框架获取真实路径
然而,解决方案似乎不再适用于Lollipop设备.
问题似乎是MediaStore.MediaColumns.DATA在ContentResolver上运行查询时返回null.
https://code.google.com/p/android/issues/detail?id=63651
您应该使用ContentResolver.openFileDescriptor()而不是尝试获取原始文件系统路径."_data"列不是CATEGORY_OPENABLE合同的一部分,因此不需要Drive返回它.
我已经阅读了CommonsWare的这篇博文,其中建议我"尝试直接使用Uri直接使用ContentResolver",这是我不明白的.如何直接在ContentResolvers中使用URI?
但是,我仍然不清楚如何最好地处理这些类型的URI.
我能找到的最佳解决方案是调用openFileDescriptor,然后将文件流复制到一个新文件中,然后将该新文件路径传递给我的上传活动.
private static String getDriveFileAbsolutePath(Activity context, Uri uri) {
if (uri == null) return null;
ContentResolver resolver = context.getContentResolver();
FileInputStream input = null;
FileOutputStream output = null;
String outputFilePath = new File(context.getCacheDir(), fileName).getAbsolutePath();
try {
ParcelFileDescriptor pfd = resolver.openFileDescriptor(uri, "r");
FileDescriptor fd = pfd.getFileDescriptor();
input = new FileInputStream(fd);
output = new FileOutputStream(outputFilePath);
int read = 0;
byte[] bytes = new byte[4096];
while ((read …Run Code Online (Sandbox Code Playgroud) android android-intent google-drive-api storage-access-framework
android ×6
android-file ×1
c ×1
c++ ×1
diskspace ×1
documentfile ×1
linux ×1
procfs ×1
sd-card ×1
unix ×1