[英]Get internal as well as external storage file path
In my app, i need to read external as well as internal filepath. 在我的应用程序中,我需要读取外部和内部文件路径。 In other words, the user can select a file from internal storage if he desires or from sdcard if he so desires.
换句话说,用户可以根据需要从内部存储中选择文件,也可以根据需要从sdcard中选择文件。
I found this while searching in SO: 我在搜索SO时发现了这一点:
Use getExternalStorageDirectory() to get access to external storage.
使用getExternalStorageDirectory()可以访问外部存储。 Use getFilesDir() to get at your app's portion of internal storage.
使用getFilesDir()获取应用程序的内部存储部分。
But how would i know whether the user has chosen a file from external or internal directory? 但是我怎么知道用户是从外部目录还是内部目录中选择文件?
Here is a sample method that reads an image from internal storage; 这是一个从内部存储器读取图像的示例方法。
public Bitmap readFromInternalStorage(String filename){
try {
File filePath = this.getFileStreamPath(filename);
FileInputStream fi = new FileInputStream(filePath);
return BitmapFactory.decodeStream(fi);
} catch (Exception ex) { /* handle exception like a king here */}
return null;
}
Now, what you could do from here is to see if the value is null and if so, then try to use a corresponding method to read from External Storage! 现在,您可以从这里开始查看该值是否为null ,如果是,则尝试使用相应的方法从外部存储中读取!
Of course, you can also have class variables to keep track of which source the image/file came from (internal or external) and update them accordingly depending on which method was called! 当然,您也可以使用类变量来跟踪图像/文件来自哪个源(内部或外部),并根据调用的方法进行相应更新!
I hope this helps you solve this issue! 希望这可以帮助您解决此问题! Good luck.
祝好运。
But how would i know whether the user has chosen a file from external or internal directory?
但是我怎么知道用户是从外部目录还是内部目录中选择文件?
That is up to you in your user interface. 这取决于您的用户界面。 You are the one who is building the file-browsing UI, and so you know what root directory you used in your file-browsing UI.
您是构建文件浏览UI的人,因此您知道您在文件浏览UI中使用了哪个根目录。
Of course, there will not be any files in any of those locations, most likely. 当然,最有可能在这些位置中的任何位置都没有文件。
getFilesDir()
is part of internal storage , and the only files that will be in there will be ones that you place there yourself. getFilesDir()
是内部存储的一部分,并且其中唯一的文件是您自己放置的文件。
getExternalFilesDir()
is part of external storage . getExternalFilesDir()
是外部存储的一部分。 In theory, the user could put files in this directory using other tools, like a desktop OS. 从理论上讲,用户可以使用其他工具(例如台式机操作系统)将文件放在此目录中。 In practice, the user is unlikely to do this, and nothing else is going to put files in that directory other than your code.
实际上,用户不太可能执行此操作,除了您的代码外,其他任何操作都不会将文件放在该目录中。
You are certainly welcome to write a file-browsing UI that uses other external storage locations, such as Environment.getExternalStoragePublicDirectory()
, that are more likely to actually contain files. 当然,欢迎您编写一个文件浏览UI,该UI使用其他外部存储位置,例如
Environment.getExternalStoragePublicDirectory()
,这些位置更可能实际包含文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.