In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to save drawable resources to the data directory in Android". In the daily operation, I believe that many people have doubts about how to save drawable resources to the data directory in Android. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the questions of "how to save drawable resources to the data directory in Android". Next, please follow the editor to study!
1. Directory permissions
By default, Android cannot directly manipulate the Data root directory and related directories, so you can only directly read and write the program's own private directory, that is, / data/data/Package name/. By default, you can only operate this directory, that is, it is impossible for us to read and write / data directly. Then under what circumstances can direct read and write / data/ be realized?
Your APP got super access. (the system is already root, and your APP has obtained super privileges)
Your application is a system-level application, and the property in the mk file has been modified to LOCAL_CERTIFICATE: = platform
The folder under / data directory is open to all users to read and write.
*: at present, many mobile apps can only be used after obtaining root. I believe you should be very clear about these, so I won't say much about them here.
Second: add the attribute android:sharedUerId= "android.uid.system" to the manifest node in the application AndroidManifest.xml. It is then compiled in the source environment and installed by adb install. The attribute in the mk file is changed to LOCAL_CERTIFICATE: = platform.
The third: directly develop the folder you need to read and write under data to all users when compiling the system.
In fact, all three methods are implemented at the system layer, because rights management is implemented by the Linux kernel. For general applications, you only need to read and write under your own private folder, but I need to do something at the system level here. If I write the file to the private folder, and the application data is emptied, the file will be gone. Therefore, you cannot directly put the private folder of the application. Because the system on my side is compiled by myself, I chose the third simple way: when compiling the system, open a file directory under data for APP to read and write. (the second method is found on the Internet, but I haven't verified it yet. * the third method is verified to be feasible)
I have created a / data/carLogo folder here and open read and write permissions to all applications.
2. Save the Drawable picture to / data/
/ / copy the picture file to the specified directory / / PicID is the image resource of drawable ID public void copyImage2Data (Integer PicID) {Log.d (TAG, "mythou copyImage2Data- > Enter PicID=" + PicID); try {/ / calculate the full path of picture storage String LogoFilePath = LogoFileRoot + LogoFileName; File dir = new File (LogoFileRoot) / / if the folder does not exist, create one (only in the directory under the application package, other directories need to apply for permission OWL) if (! dir.exists ()) {Log.d (TAG, "mythou copyImage2Data- > dir not exist");} boolean result = dir.mkdirs () Log.d (TAG, "dir.mkdirs ()-> result =" + result); / / get the InputStream object of the encapsulated file InputStream is = mContext.getResources (). OpenRawResource (PicID); Log.d (TAG, "copyImage2Data- > InputStream open"); FileOutputStream fos = new FileOutputStream (LogoFilePath) Byte [] buffer = new byte [8192]; System.out.println ("3"); int count= 0; / / start copying Logo image file while ((count=is.read (buffer)) > 0) {fos.write (buffer, 0, count) System.out.println ("4");} fos.close (); is.close ();} catch (Exception e) {e.printStackTrace ();}}
The above is to copy the image resources specified under our drawable to the specified directory under / data/, mainly using openRawResource () and FileOutputStream () methods to read drawable resources and write file streams, respectively.
3. Read pictures from / data/
/ / url is the specific file path / / for example here: / data/cardLogo/logo.gif public Bitmap getLoacalBitmap (String url) {try {/ / Open the picture file to a file stream and decode it to bitmap FileInputStream fis = new FileInputStream (url); return BitmapFactory.decodeStream (fis) } catch (FileNotFoundException e) {e.printStackTrace (); return null;}}
With the above method, we can read the image file of the path specified by / data, and then decode it into a Bitmap object that we can use. The Bitmap object can be constructed into a drawable class through the BitmapDrawable class, which can then be used to bind various View controls.
/ / obtain the picture file under data through the above method, and decode it into the Bitmap object Bitmap logoBK = getLoacalBitmap (mCopyFile.getLogoPicPath ()); / / construct the drawable object, BitmapDrawable is a subclass of Drawable BitmapDrawable logoDrawable=new BitmapDrawable (logoBK); / / set the background of ImageView to the specified picture mLogoImageView.setBackgroundDrawable (logoDrawable); at this point, the study on "how to save drawable resources to the data directory in Android" is over. I hope you can solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.