File Storage path

Source: Internet
Author: User

Quick query of IP Phone logistics train applicationsProgram
Http://www.eoeandroid.com/thread-201970-1-1.html

CompleteSource codeExplanation-chat, file transmission, voice call, android...
Http://www.eoeandroid.com/thread-201406-1-1.html

[EOE's first Android Talent Training Camp] started!
Http://www.eoeandroid.com/thread-198942-1-1.html

Android Developer Conference

Http://www.eoeandroid.com/special/myway? Pc_hash = occomh

Determine whether the application process is single-threaded and multi-core parallel programming
Http://www.eoeandroid.com/thread-202149-1-1.html

 

Static Data carried by an application can be stored in the assets directory or res and raw directory of the application.
Static Data in the Assets Directory has the following restrictions when the file supports a maximum of 1 MB.:

 

 
Inputstream is = getassets (). Open ("XXX. xx ");

 

Files in the res and raw directories can be read as follows:

 

 
Inputstream is = resources. openrawresource. (R. Raw. XXX );

 

In addition, Android also supports cache creation by using getcachedir () you can obtain the application cache path . When the system space is insufficient, Android clears the cache, but developers should not maintain too much cache. Users can manually clear the cache through system settings. Some people also use this cache to cache images. However, although there is less traffic, however, for program efficiency, it only shortens the download time of a network. Developers can choose whether to use the cache space based on their application features.

Android supports private app directories on the SD card, after froyo, you can use getexternalfilesdir () to obtain the specific path . This path depends on the package name of the application. If your package is hello. file, the private directory of the application on SD is \ MNT \ sdcard \ Android \ data \ hello. file \ files \. If you want to store it in the public directory on the SD card, you can use getexternalstoragepublicdirectory () the specific path of the public directory depends on the file type to be stored.

Applications on SD require large caches, you can also create a cache directory on the SD card. after froyo, you can use getexternalcachedir () to obtain the cache path of the application on the SD card . When using the SD card directory, pay attention to whether the SD card is mounted. You can use environment. getexternalstoragestate () method. If the returned value is envirnment. media_mounted indicates that the SD card is mounted and can be safely used.
transactions in SQLite. when you need to modify multiple data at a time, we recommend that you use SQLite transaction processing for batch processing, which improves the execution efficiency. The transaction processing method is as follows:

 

 
Public VoidBegintransaction ();Public VoidBegintransactionwithlistener (sqlitetransactionlistener transactionlistener );Public VoidEndtransaction ();Public VoidIntransaction ();Public VoidSettransactionsuccessful ();

 

 

 

In addition, XML files stored using sharedpreferencesThe essence is actually a <key, value> value pair. XML files in Android are more used to maintain application or system configuration files. In development, sharedpreferences is used to save application configuration information and record some user operations, for example: whether the user first enters the application, whether the user has chosen to save the user name and password, and some user preferences. Whether Android is open to other applications based on the configuration information, sharedpreferences provides two permissions,Mode_private and mode_world_readable.The data is stored in the \ data \ package. Name \ shared_prefs folder.The file extension is. xml. Data is stored in this form:

<Map> <Boolean name = "firstflag" value = "true"/> </map>
Method for obtaining sharedpreferences: (this variable is generally used as a global variable and put into the oncreate method of activity for initialization. I encountered an error where someone could not get this variable in the method, the result is global .)

 

 
Sharedpreferences sp = getsharedpreferences ("Name of the stored XML file", mode_private );

 

How to obtain data:

 

 

 
String MSG = sp. getstring ("stored key", "data obtained if not saved ");

 

MSG is the data you saved. If it is not saved, the obtained data is the default data you set later.
The editing method is as follows:

 
Editor editor =Sp. Edit (); editor. putstring (Key, value); editor. Commit ();//Do not forget to submit the file. Otherwise, the file will not be saved.

If you want to open the configuration information to other applications,You can useMode_world_readable,To obtain the configuration information, other applications must first obtain the corresponding context:

 
Context = createpackagecontext ("all-inclusive name", Context_context_ignore_security); sharedpreferences sp= Context. getsharedpreferences ("Name of the stored XML file", mode_private );

In actual development, you must determine whether to obtain the context to be null. In some cases, we cannot open the context, or the application has been deleted due to insufficient permissions.
You must note that the preferenceactivity has built-in support for sharedpreferences. If you want to learn more, you can view usage similar to listpreference.
Android also provides an internal storage mode,Developers can store data in the private space of the application to avoid access by other programs. Its permissions are in the context and include:Mode_private, mode_append, mode_world_readable, mode_world_writeable. InternalDirectory:/date/package. Name/Files
Writing readable data is also simple:

 
Fileoutputstream Fos =Context. openfileoutput (file, context_mode_world_writeable); FOS. Write (bytes); FOS. Close (); fileinputstream FCM= Context. openfileinput ("FILENAME ");

 

Next, read the file as usual.

 

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.