Android:消除文件路径歧义 [英] Android: disambiguating file paths

查看:103
本文介绍了Android:消除文件路径歧义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,用户选择文件.在内部,我存储有关文件的信息,我根据文件路径键入该信息.下次使用该文件时,我将使用存储的信息进行处理.问题是我使用以下方法实例化文件:

In my app, users pick files. Internally, I store information about the file, which I key based on the file path. Next time that file is used, I do stuff with the stored information. Trouble is I instantiate my files with:

文件file1 =新文件(Environment.getExternalStorageDirectory()+"/test.txt");

然后,在特定的JB设备上,file1.getCanonicalPath()给出:"/storage/emulated/0/test.txt".

And then, on a particular JB device, file1.getCanonicalPath() gives: "/storage/emulated/0/test.txt".

问题在于,当其他应用程序在Intent中使用文件路径启动我的应用程序时,它们发送的路径通常看起来像:"/mnt/sdcard/test.txt".

The trouble is that when other apps launch my app with a file path in an Intent, the paths they send tend to look like: "/mnt/sdcard/test.txt".

是否有一个明智的策略来消除这两个路径的歧义?可能我应该以不同的方式实例化我的文件?

Is there a smart strategy to disambiguate these two paths? Possibly I should be instantiating my files differently?

麻烦的是,两个文件的两个规范路径不相等.对于以下内容, cp1 =="mnt/sdcard/test/txt" cp2 =="/storage/emulated/0/text/txt" :

The trouble is, the two canaonical paths for the two files are not equal. For the below, cp1=="mnt/sdcard/test/txt" and cp2=="/storage/emulated/0/text/txt":

File file1 = new File("/mnt/sdcard/test.txt");
File file2 = new File("/storage/emulated/0/test.txt");

String cp1 = file1.getCanonicalPath();
String cp2 = file2.getCanonicalPath();

推荐答案

首先,获取外部路径的唯一正确方法是使用 getExternalStorageDirectory 和其他 getExternalStorageXXX 安卓.

First, the only correct way to get the external path is using the getExternalStorageDirectory and other getExternalStorageXXX in Android.

Android首先会尝试解析两个系统变量:

Android will firstly try to resolve two system variable:

String rawExternalStorage = System.getenv(ENV_EXTERNAL_STORAGE);
String rawEmulatedStorageTarget = System.getenv(ENV_EMULATED_STORAGE_TARGET);

同时 ENV_EXTERNAL_STORAGE ="EXTERNAL_STORAGE" ENV_EMULATED_STORAGE_TARGET ="EMULATED_STORAGE_TARGET" .如果设置了 EMULATED_STORAGE_TARGET 变量,则表示设备已仿真存储,则存储路径将为 EMULATED_STORAGE_TARGET .(Android 4.2之后,它支持多用户外部存储.路径后会有/0或0),但是如果未设置并且设置了 EXTERNAL_STORAGE ,则路径将是 EXTERNAL_STORAGE .如果未同时设置它们,则默认情况下路径为/storage/sdcard0 .因此,不同的设备可能包含用于外部存储的不同路径.

while the ENV_EXTERNAL_STORAGE = "EXTERNAL_STORAGE" and ENV_EMULATED_STORAGE_TARGET = "EMULATED_STORAGE_TARGET". If the EMULATED_STORAGE_TARGET variable is set, it means the device has emulated storage then the storage path will be EMULATED_STORAGE_TARGET.(After Android 4.2, it support multiple-user external storage, then there would be a /0 or 0 after the path) But if it is not set and EXTERNAL_STORAGE is set, the path will be EXTERNAL_STORAGE. If both of them are not set, the path will be /storage/sdcard0 by default. So different devices may contain different paths for external storage.

外部存储技术信息所述,您可以通过设置init.rc来自定义设备的存储.文件.例如,在默认的金鱼之一中:

As External Storage Technical Information says, you can customize the storage of the device by setting up the init.rc file. For example in the default goldfish one:

export EXTERNAL_STORAGE /mnt/sdcard
mkdir /mnt/sdcard 0000 system system
symlink /mnt/sdcard /sdcard

如果使用 getExternalStorageDirectory ,则将获得/mnt/sdcard ,但是/sdcard 是指向该目录的符号链接.

If you use getExternalStorageDirectory you will get /mnt/sdcard, but /sdcard is a symbolic link to that directory.

因此,在您的情况下,init.rc可能包含:

So in your case, the init.rc may contain:

export EMULATED_STORAGE_TARGET /storage/emulated
symlink /storage/emulated/0 /mnt/sdcard

因此它们不是模棱两可的,实际上它们是相同的.

So they are not ambiguous, they are actually same.

我认为 getCanonicalPath()可能适用于绝大多数用例.

I think the getCanonicalPath() might work for the vast majority of your use cases.

规范路径名是绝对的,也是唯一的.精确的规范形式的定义取决于系统.首先这个方法如有必要,将此路径名转换为绝对形式,就像通过调用getAbsolutePath()方法,然后将其映射到其唯一以系统相关的方式形成表格.这通常涉及删除多余的名称,例如."和路径名称中的"..",解析为符号链接(在UNIX平台上),并将驱动器号转换为标准案例(在Microsoft Windows平台上).

A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as "." and ".." from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

表示现有文件或目录的每个路径名都有唯一的规范形式.表示不存在的文件的每个路径名,或者目录也具有唯一的规范形式.的规范形式不存在的文件或目录的路径名可能与文件或目录后的相同路径名的规范形式创建.同样,现有路径名的规范形式文件或目录可能与同一文件或目录的规范形式不同删除文件或目录后的路径名.

Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted.

这篇关于Android:消除文件路径歧义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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