Android 资源保护问题——探索
apk文件使用解压工具就能看到drawable等资源,但是有些游戏中的图片资源却是无法看到的。
这个问题探索了许久……
【1】图片资源不放置在drawable文件下,放在assets中(但是解压apk,同样能看到图片资源),以下说说使用方法。
分析:Ⅰ)当图片资源放在drawable中的时候,能有相应的Id去解析: BitmapFactory.decodeResource(res, id)
如果放置在assets下,就需要根据文件的名字去解析(Android提供AssetManager)。
Ⅱ)可以自己建立多层目录,方便管理。
Ⅲ)这样的解析过程,耗费的时间要比根据Id解析要多(手机越来越智能,这点时间基本看不出来)。
代码:
/**
* 从Assets中读取图片
* @param fileName :assets根目录下 "a.png",有子文件夹的 "abc/a.png"
* @return
*/
public static Bitmap getImageFromAssets(Context context, String fileName)
{
Bitmap image = null;
AssetManager am = context.getResources().getAssets();
try
{
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
}
【2】图片资源打包在jar下,然后导入工程(但是解压apk,同样能看到图片资源)
分析:使用过一下第三方的jar包,在apk解压后是看不到的,尝试看看。最终发现jar包中的assets文件在apk中可见了。
Step1:打包jar
工程->右键->Export->Java/jar file->选择需要打包的src 和 assets(如下图)

Step2:解读assets中的图片,同【1】
Step3:打包apk,然后发现jar包中的assets和当前工程的assets合并了!
【3】图片资源加密,然后在assets文件下读取(可以实现资源保护,但是貌似比较耗时)
分析:通过某种方式对图片预先加密,然后在Android程序中解密,在转换成Bitmap。
可能别的应用程序就是这样做的吧,哪位大神有妙招,给介绍一下吧!(下面介绍一下简单方法)
Step1:加密,采用文件流方式,读取资源,然后修改,最后生成文件(随便格式都可以,就不能知道是图片了)
Ⅰ)每隔多少个字节添加一个指定的字节
Ⅱ)每隔多少个字节,交换字节(代码示例)
Step2:解密,与加密过程反向即可。
/**
* 从Assets中读取图片
* @param fileName
* @return
*/
public static Bitmap getImageFromAssets(Context context, String fileName)
{
Bitmap image = null;
AssetManager am = context.getResources().getAssets();
try
{
InputStream is = am.open(fileName);
byte[] buffer = new byte[1000000];//足够大
is.read(buffer);
for(int i=0; i<buffer.length; i+= 5000){//与加密相同
byte temp = buffer[i];
buffer[i] = buffer[i+1];
buffer[i+1] = temp;
}
image = BitmapFactory.decodeByteArray(buffer, 0, buffer.length);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
}
【3】使用setPixel()和getPixel()对每个像素点进行加密,然后在使用的时候在还原
分析:通过Bitmap.getPixel(x, y)得到color值,对color的rgb值加密操作,然后setPixel(x,y,color)
Step1:懒得写了,直接贴代码:
注意:bitmap一定要copy一份,然后第二个值为true才能对其setPixel,不然会报错的;代码中的encrypt和decrypt就是你加密解密过程;
严重问题:对bitmap setPixel然后在getPixel,color值竟然不是set的值,有偏差,不知道为什么。有能解决这个问题的,请留言一下。
Bitmap temp_bitmap = image.copy(Bitmap.Config.ARGB_8888, true);
int width = temp_bitmap.getWidth();
int height = temp_bitmap.getHeight();
int[] pixels = new int[width * height];
//temp_bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
int color = temp_bitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int alpha = Color.alpha(color);
//if(alpha != 0)
{
r = encrptyRGB(r, 2*(i*j));
g = encrptyRGB(g, 4*(i*j));
b = encrptyRGB(b, 6*(i*j));
color = Color.argb(alpha, r, g, b);
pixels[width * i + j] = color;
//temp_bitmap.setPixel(i, j, color);
}
}
}
temp_bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
for(int i = 0; i < height; i++)
{
for(int j = 0; j < width; j++)
{
int color = temp_bitmap.getPixel(i, j);
int r = Color.red(color);
int g = Color.green(color);
int b = Color.blue(color);
int alpha = Color.alpha(color);
//if(alpha != 0)
{
r = decryptRGB(r, 2*(i*j));
g = decryptRGB(g, 4*(i*j));
b = decryptRGB(b, 6*(i*j));
color = Color.argb(alpha, r, g, b);
pixels[width * i + j] = color;
//temp_bitmap.setPixel(i, j, color);
}
}
}
temp_bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
return temp_bitmap;
【?】继续探索中,忘大神指教!感激不尽!
Android 资源保护问题——探索的更多相关文章
- Android开发艺术探索读书笔记——01 Activity的生命周期
http://www.cnblogs.com/csonezp/p/5121142.html 新买了一本书,<Android开发艺术探索>.这本书算是一本进阶书籍,适合有一定安卓开发基础,做 ...
- Android开发艺术探索笔记——View(二)
Android开发艺术探索笔记--View(二) View的事件分发机制 学习资料: 1.Understanding Android Input Touch Events System Framewo ...
- Android开发艺术探索笔记—— View(一)
Android开发艺术探索笔记 --View(一) View的基础知识 什么是View View是Android中所有控件的基类.是一种界面层控件的抽象. View的位置参数 参数名 获取方式 含义 ...
- android开发艺术探索
android开发艺术探索 百度任玉刚 http://blog.csdn.net/singwhatiwanna/article/details/46810527
- 《Android开发艺术探索》读书笔记 (13) 第13章 综合技术、第14章 JNI和NDK编程、第15章 Android性能优化
第13章 综合技术 13.1 使用CrashHandler来获取应用的Crash信息 (1)应用发生Crash在所难免,但是如何采集crash信息以供后续开发处理这类问题呢?利用Thread类的set ...
- 《Android开发艺术探索》读书笔记 (9) 第9章 四大组件的工作过程
第9章 四大组件的工作过程 9.1 四大组件的运行状态 (1)四大组件中只有BroadcastReceiver既可以在AndroidManifest文件中注册,也可以在代码中注册,其他三个组件都必须在 ...
- 《android开发艺术探索》读书笔记(十五)--Android性能优化
接上篇<android开发艺术探索>读书笔记(十四)--JNI和NDK编程 No1: 如果<include>制定了这个id属性,同时被包含的布局文件的根元素也制定了id属性,那 ...
- 《android开发艺术探索》读书笔记(十四)--JNI和NDK编程
接上篇<android开发艺术探索>读书笔记(十三)--综合技术 No1: Java JNI--Java Native Interface(java本地接口),它是为了方便java调用C. ...
- 《android开发艺术探索》读书笔记(十三)--综合技术
接上篇<android开发艺术探索>读书笔记(十二)--Bitmap的加载和Cache No1: 使用CrashHandler来获取应用的crash信息 No2: 在Android中单个d ...
随机推荐
- STL源码剖析——hashtable
二叉搜索树具有对数时间的搜索复杂度,但是这样的复杂度是再输入数据有足够的随机性的假设上哈希表在插入删除搜索操作上也具有常数时间的表现,而且这种表现是以统计为基础,不需要依赖输入元素的随机性 hasht ...
- Python操作dict时避免出现KeyError的几种方法
见原文:https://www.polarxiong.com/archives/Python-%E6%93%8D%E4%BD%9Cdict%E6%97%B6%E9%81%BF%E5%85%8D%E5% ...
- linux系统的磁盘挂载
1.查看数据盘在没有分区和格式化数据盘之前,使用 “df –h”命令,是无法看到数据盘的,可以使用“fdisk -l”命令查看.如下图:2. 对数据盘进行分区执行“fdisk /dev/xvdb”命令 ...
- python爬虫——利用BeautifulSoup4爬取糗事百科的段子
import requests from bs4 import BeautifulSoup as bs #获取单个页面的源代码网页 def gethtml(pagenum): url = 'http: ...
- Dialog 样式 主题 标题 背景 使用【总结】
最重要的是这两行代码 dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);//设置Dialog没有标题,需在setContentView之前设置 ...
- Sqlserver 使用CTE如何按子查询排序?
需求:查出最近有更改的客户信息(按最后更改时间排序,来自SystemLog表LogDateTime字段) 说明: Customer:客户信息表SystemLog:系统日志表,记录所有表信息的增,删,改 ...
- Caused by: java.lang.NumberFormatException: For input string: ""
1.错误描写叙述 java.lang.NumberFormatException: For input string: "" at java.lang.NumberFormatEx ...
- quartz.net持久化和集群【转】
在实际使用quartz.net中.持久化能保证实例重启后job不丢失. 集群能均衡服务器压力和解决单点问题. quartz.net在这二块配置都比较方便,来看下. 一:持久化 quartz.net的持 ...
- 用mapreduce来操作hbase的优化
(1)scan.setCacheBlocks(false); 初始化map任务 TableMapReduceUtil.initTableMapperJob 本次mr任务scan的所有数据不放在缓 ...
- android路径获取
//内部路径 Environment.getDataDirectory()=/data Environment.getDownloadCacheDirectory()=/cache Environme ...