Internal storage, External Storage concepts, file operations in Android are somewhat different from pc-side
Source: Internet
Author: User
<span id="Label3"></p><p><p>In fact, the operation of the Android file and Java in the PC environment is not two, the reason why need to be explained separately is because the Android system provides different from the Pc's access to the file system root path of the api, while the application of a private file to do a unified management. Beginners in this section feel it is easy to confuse the two concepts of internal storage and external Storage.</p></p><p><p><strong>Relative path and absolute path</strong></p></p><p><p>In java, the relative path and absolute path are interpreted in this way.</p></p><p><p>An absolute path is the full path to a written file, such as d:\java\Hello.java, which contains the full path of the file D:\java and the full name of the file Hello.java. Using this path, you can find only one file without Ambiguity. however, the use of absolute paths in representing files is very restrictive and cannot be run under different operating systems because absolute paths are expressed differently under different operating Systems.</p></p><p><p>A relative path is a partial path to a written file, such as \test\hello.java, which contains only part of the path to the file \test and the full name of the file hello.java, some of which are sub-paths under the current path, such as the current program running under D:\ABC. Then the full path to the file is D:\abc\test. Using this form, you can more generally represent the location of the file, resulting in a certain degree of flexibility in the file path.</p></p><p><p>When you run a program in an Eclipse project, the current path is the root of the project, such as when the workspace is stored in D:\javaproject and the current project name is Test, the current path is: D:\javaproject\Test. When running a program under the console, the current path is the directory where the class file resides, and if the class file contains the package name, the package name at the top of the class file is the current path.</p></p><p><p> <span style="color: #ff0000;">This is what Java does in most operating systems, and it is clear that we use relative paths as much as possible, but in android, in most cases we are using absolute paths. </span>for what? Note that the relative path is the current path as the current project, but in Android we are not able to do anything in the path directory of the project, because ordinary Java our project is created in the server (pc is a server), running on the server, we can of course, the server operating its own file Directory. But Android development, our project is generally created in their own work of the computer, and running on the mobile phone, since the APK has been running on the phone, the project has been deployed to the mobile phone, should be in the location of the APK on the phone to determine the relative path, but we seem to have no way to operate this path, Because the APK is in the system directory, even if it can be manipulated, access to files in this directory is meaningless, such as I write a photo album program, the picture must be placed in the external storage, and if I want to save some of the App's settings data, I was placed in the internal storage of the database directory, So in fact, in the Android file management, we are operating the absolute path.</p></p><p><p><strong><strong>File class</strong></strong></p></p><p><p>The operation of a file (read/write, create a file or Directory) is done through the file class, which is exactly the same as in Java.</p></p><p><p><strong>External storage <strong>External storage</strong> and internal storage <strong>Internal <strong>storage</strong> </strong></strong></p></p><p><p><strong>1. Internal Storage:</strong></p></p><p><p>Note Internal storage is not memory. Internal storage is a very special location in the system, and if you want to store the files in an internal store, the files are only accessible to your app by default, and all files created by an app are in the same directory as the app package Name. This means that the application is created in the internal storage file, which is associated with the Application. When an app is uninstalled, these files in the internal storage are also Deleted. technically, If you set the file property to be readable when you create the internal storage file, other apps can access your App's data, provided that he knows the package name of your app, and if the property of a file is private, then even if you know the package name, other apps won't be able to access it. The internal storage space is very limited, so it is valuable, in addition, it is the system itself and the system application of the main data storage location, once the internal storage space is exhausted, the phone will not be used. So for the internal storage space, we should try to avoid it. Shared preferences and SQLite databases are stored on the internal storage space. Internal storage is generally used to obtain and manipulate the Context.</p></p><p><p>Getfilesdir () Gets the internal storage space of your app, which is the root of your app's internal storage.</p></p><p><p>If you are creating a file, as follows: file File = newFile (context.getfilesdir (), filename);</p></p><p><p>Android also provides us with an easy way <code>openFileOutput()</code> to read and write files that are applied to the internal storage space, and here is an example of writing text to a file:</p></p><pre><pre>String filename = "myfile"<span style="color: #000000;"></span>= "Hello world!" <span style="color: #000000;">; FileOutputStream outputstream;</span> <span style="color: #0000ff;">Try</span> <span style="color: #000000;"></span> =<span style="color: #000000;"></span><span style="color: #0000ff;">catch</span><span style="color: #000000;">(Exception e) {e.printstacktrace ();}</span></pre></pre><p><p>Some additional operations for internal storage:</p></p><p><p>A. List all the files that have been created, which may not be easy to imagine, <code class="js plain">Context</code> actually have such a method.</p></p><pre><pre>string[] files =<span style="color: #000000;"> context.filelist ();</span> <span style="color: #0000ff;"></span> for <span style="color: #000000;"></span> "file is" +<span style="color: #000000;"> file);}</span></pre></pre><p><p>B. Delete files, can be created to be able to delete, of course, also provides the interface to delete files, It is very simple, only need to provide file name</p></p><pre><pre><span style="color: #0000ff;">If</span><span style="color: #000000;"></span><br><span style="color: #0000ff;">else</span><span style="color: #000000;"></span>"failed to deletefile" +<span style="color: #000000;"> filename);}</span></pre></pre><p><p>C. Create a directory, you need to pass in the directory name, it returns a file object to the action path</p></p><pre><pre>File Workdir =<span style="color: #000000;"></span>"workdir" + workdir.getabsolutepath ();</pre></pre><p><p>Summarize the file related operations, you can draw the following three features:<br>1. File operation only need to provide a file name to the function, so the program itself only needs to maintain the file name;<br>2. No need to create file object and input, output stream, provide file name can return file object or input output stream<br>3. The file object is returned for the path Operation.</p></p><p><p><strong>2. External storage:</strong></p></p><p><p>The most confusing is the external storage, if the PC is also to distinguish between external storage and internal storage, then the drive is internal storage, u disk or mobile hard disk is external storage, so we can easily take this understanding to look at the Android phone, think that the intrinsic storage of the fuselage is internal storage, The extended T card is external storage. For example, we think that the 16GB version of the Nexus 4 has 16G of internal storage, ordinary consumers can understand this, but Android programming can not, this 16GB is still external storage.</p></p><p><p>All Android devices have external storage and internal storage, both of which are from the early devices of android, where the Device's internal storage is really fixed, and the external storage can be moved like a USB stick. But in later devices, many Medium-and high-end machines have extended their own body storage to more than 8G, they will be stored in the concept of "internal internal" and "external external" two parts, but in fact, are inside the PHONE. So regardless of whether Android phones have removable sdcard, they always have external storage and internal storage. Most crucially, we all use the same API to access removable sdcard or the storage that comes with the phone (external storage).</p></p><p><p> <span style="color: #ff0000;">Although the concept of external storage is a little complicated, but also very good to distinguish, you connect your phone to the computer, the computer can be recognized by the part of the external storage must be.</span></p></p><p><p><span style="color: #000000;">Files in external storage can be modified by users or other applications, and there are two types of files (or directories):</span></p></p><p><p><span style="color: #000000;">1. Public file common files: files can be accessed freely, and the data of the file is meaningful to other applications or users, and the files created before uninstallation are retained when the application is Uninstalled. For example, the camera app, the generated photos can be accessed by everyone, and the camera is gone, the photos are still there.</span></p></p><p><p><span style="color: #000000;">If you want to put public files on the external storage you can use <strong>getexternalstoragepublicdirectory ()</strong></span></p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #000000;">File getalbumstoragedir (String albumname) { </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> Get The directory for the user ' s public pictures di Rectory.</span> File File =<span style="color: #000000;"> newFile (environment.getexternalstoragepublicdirectory (environment.directory_pictures), albumName); </span> <span style="color: #0000ff;">if</span>(!<span style="color: #000000;"></span>" Directory not created "<span style="color: #000000;">); } return file;</span></pre></pre><p><p>In the above code we create a directory where the picture is stored and create a new Albumname File.</p></p><p><p><span style="color: #ff0000;">If your API version is less than 8, you cannot use <strong>getexternalstoragepublicdirectory ()</strong>, but instead use <code class="js plain">Environment</code> . getexternalstoragedirectory (), with no parameters , you cannot create a directory on your own, just return the root path of the external store.</span></p></p><p><p><span style="color: #000000;">2. Private file: in fact, because it is the reason for external storage even this type of file can be accessed by other programs, but an application of private files to other applications is actually no access value (except for malicious programs). On external storage, The value of applying a private file is that the files are deleted after they are Uninstalled. Similar to internal storage.</span></p></p><p><p><span style="color: #000000;">The way to create an app private file is<code class="js plain"><code class="js plain">Context.getExternalFilesDir(),如下:</code></code></span></p></p><p><p></p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #000000;">File getalbumstoragedir (context context, String albumname) { </span> <span style="color: #008000;">//</span> <span style="color: #008000;"> Get The directory for the app ' s PR Ivate Pictures directory.</span> File File =<span style="color: #000000;"> newFile (context.getexternalfilesdir (environment.directory_pictures), albumname); </span> <span style="color: #0000ff;">if</span>(!<span style="color: #000000;"></span>" Directory not created "<span style="color: #000000;">); } return file;</span></pre></pre><p><p>The above code creates a picture directory and creates a file named in this directory <code class="js plain">albumName</code> , which <code class="js spaces"></code> <code class="js plain">Environment.DIRECTORY_PICTURES</code> is actually the string picture.</p></p><p><p>The private files stored externally for all applications are placed under the android/data/of the root directory, in the form of/android/data/<em><package_name></em>/</p></p><p><p>If your API version is less than 8, then you <strong> <code class="js plain"><code class="js plain">getExternalFilesDir()</code></code> </strong> cannot use it, but use the <code class="js plain">Environment</code> . getexternalstoragedirectory () to get the root path and try to manipulate it yourself/android/data/ <em> <package_name></em>/under The File.</p></p><p><p><span style="color: #ff0000;">In other words, the following versions of API 8 do not provide API support specifically for the operation of files and private Files. You can only get the root directory and then do it yourself.</span></p></p><p><p><span style="color: #000000;">Before using external storage, you must first check the current state of the external storage to determine if it is available.</span></p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>Mexternalstorageavailable =<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">;</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Boolean</span></span>Mexternalstoragewriteable =<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">; String</span> state</span>=<span style="color: #000000;"><span style="color: #000000;">environment.getexternalstoragestate ();</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">if</span></span><span style="color: #000000;"><span style="color: #000000;">(Environment.MEDIA_MOUNTED.equals (state)) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">We can read and write the media</span></span>mexternalstorageavailable = Mexternalstoragewriteable =<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">;} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals (state)) {</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">We can only read the media</span></span>Mexternalstorageavailable =<span style="color: #0000ff;"><span style="color: #0000ff;">true</span></span><span style="color: #000000;"><span style="color: #000000;">; Mexternalstoragewriteable</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">;} </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Else</span></span><span style="color: #000000;"><span style="color: #000000;">{</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;"><span style="color: #008000;">Something Else is wrong. It may be one of the many other states, but all we need</span></span><span style="color: #008000;"><span style="color: #008000;">//</span></span><span style="color: #008000;">to <span style="color: #008000;">know is we can neither read nor write</span></span>mexternalstorageavailable = Mexternalstoragewriteable =<span style="color: #0000ff;"><span style="color: #0000ff;">false</span></span><span style="color: #000000;"><span style="color: #000000;">;}</span></span></pre><p><p></p></p><p><p>Internal storage, External Storage concepts, file operations in Android are somewhat different from pc-side</p></p></span>
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service