简体   繁体   English

获取内部和外部存储文件路径

[英]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.

相关问题 在Android中使用绝对路径获取内部和外部存储中所有文件(每个文件)的列表 - Get list of all files (every file) in both internal and external storage with absolute path in Android 从Android中的外部存储获取文件路径 - Get path of file from External Storage in Android 如何从android中的内部存储中获取文件的文件路径 - How to get file path of file from internal storage in android 获取“外部存储”的所有路径的列表时,哪个路径指向内部存储? - Which path point to the internal storage when get a list of all paths of the “external storage”? 在Android中的外部存储和内部存储上存储文件 - Storing file on External Storage and Internal Storage in android afilechooser不返回带有外部和内部存储的设备的路径 - afilechooser not returning path for device with external and internal storage Android - 如何从内部存储中获取所有文件路径或从内部存储中删除所有文件 - Android - How to get all file path from internal storage or delete all files from internal storage 如何使用Android中的MediaStore.Files从内部和外部存储中获取所有.pdf文件 - How can I get all .pdf files from Internal as well as External storage using MediaStore.Files in Android 获取内部文件路径 - Get internal file path 外部存储到内部存储 - External storage to internal storage
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM