Now most of the Android app's picture resources, after the anti-compilation can be directly used, if you do not want to make their own image resources directly after the use of anti-compilation, the first thought should be the image encryption. Here the author, hastily wrote a simple way to encrypt the image, I hope you crossing useful.
The first is the encryption section, where a simple XOR is used to encrypt the image. Create a new Java project or simply write down the following code with a text file, and then execute it at the command line with the following code:
1 Import java.io.File; 2 3 Import Javax.imageio.stream.FileImageInputStream; 4 Import Javax.imageio.stream.FileImageOutputStream; 5 Import Javax.imageio.stream.ImageInputStream; 6 Import Javax.imageio.stream.ImageOutputStream; 7 8 public class Encriptdemo {9, public static final int xor_const = 0x99;//Key All/**13 * @param args1 4 */15 public static void Main (string[] args) {The file load = new file ("/home/jade/desktop/loading.png"); 18 File Loaddest = new file ("/home/jade/desktop/loading.dat"), File Loadd = new file ("/home/jade/Desktop/LOADING_DEP R.png "), try {encrimg (load, loaddest), Encrimg (Loaddest, Loadd); CH (Exception e) {e.printstacktrace ();}27}28 public static void Encrimg (File src, Fi Le Dest) throws Exception {Imageinputstream fis = new Fileimageinputstream (src); imageoutputstream fo s = new Fileimageoutputstream (dest); 32read;34 int (read = Fis.read ()) >-1) {fos.write (read ^ xor_const); 36 }37 Fos.flush (); Fos.close (); Fis.close (); 40}41 42}
The above code does not make the existence of the file is judged, because this code is to encrypt the file itself, so that the file exists under its own check can be. Familiar with the different or friends should want to think, in fact, the above encrimg (file src, file dest) method can do both encryption, and can do decryption, in main made two calls, the first time to encrypt, the second time to decrypt. So the encryption program is finished, and then look at the decryption method in Android (the above encryption code, using the Javax.imageio.stream package of related classes, so the default cannot be executed in Android project).
Copy the newly encrypted Loading.dat file to the assets folder of your Android project and add a Imageview,id to IMG in the layout so that you can execute the following code in the activity:
1 ImageView img = (ImageView) Findviewbyid (r.id.img), 2 Bitmap Bitmap = Imageadapter.readbitmap (This, "Loading.dat"); 3 if ( Bitmap! = null) {4 img.setimagebitmap (bitmap); 5} else {6 System.out.println ("Picture is empty"); 7}
Here called the Imageadapter static method Readbitmap, the method reads the author here is just a demo, so do not consider the ANR processing, the formal project is best to put the picture loading and decryption part in a separate thread execution. The code for Imageadapter is as follows:
1 Import java.io.IOException; 2 Import Java.io.InputStream; 3 Import java.util.ArrayList; 4 Import java.util.List; 5 6 Import Android.content.Context; 7 Import Android.graphics.Bitmap; 8 Import Android.graphics.BitmapFactory; 9 public class Imageadapter {One to ten public static Bitmap Readbitmap (context context, String FileName) {B Itmap bitmap = null;14 list = new ArrayList (); try {InputStream is = Context.getassets (). open (fileName); + int read;18 while (read = Is.read ()) >-1) {read = read ^ 0x99;20 list.add ((byte) read); 21 }22 byte[] arr = new byte[list.size ()];24 int i = 0;25 F or (Byte item:list) {arr[i++] = item;27}28 bitmap = Bitmapfactory.decodeb Ytearray (arr, 0, List.size ()), System.out.println (bitmap), and the catch (ioexception e) {e.printstacktrace ();}33 return bitmap;34}35 36}
The author's method, is not the best way, here is just a point, to the need to protect their own picture resources of friends a simple idea.
Http://www.cnblogs.com/error404/archive/2013/06/03/3115492.html
Android simple encryption protects your own picture resources