这些方法在开发中用的上,在这里记录一下,以后查阅。
转载请注明出处:http://heji.iteye.com/blog/757556,谢谢
Bitmap转换成Byte数组:
static byte[] bitmapToBytes(Bitmap bitmap) {
int size = bitmap.getWidth() * bitmap.getHeight() * 4;
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
return out.toByteArray();
} catch (IOException e) {
return null;
}
}
把View转换成Bitmap
/**
* 把一个View的对象转换成bitmap
*/
static Bitmap getViewBitmap(View v) {
v.clearFocus();
v.setPressed(false);
//能画缓存就返回false
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = v.getDrawingCache();
if (cacheBitmap == null) {
Log.e(TAG, "failed getViewBitmap(" + v + ")", new RuntimeException());
return null;
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}
Stream转换成Byte
static byte[] streamToBytes(InputStream is) {
ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
byte[] buffer = new byte[1024];
int len;
try {
while ((len = is.read(buffer)) >= 0) {
os.write(buffer, 0, len);
}
} catch (java.io.IOException e) {
}
return os.toByteArray();
}
BitmapDrawble to Bitmap :
getBitmap()
Bitmap to BitmapDrawable :
public static BitmapDrawable bitmapToBitmapDrawable(Bitmap bitmap, Context context) {
return new BitmapDrawable(context.getResources(), bitmap);
}
/** 重新编码Bitmap
*
* @param src
* 需要重新编码的Bitmap
*
* @param format
* 编码后的格式(目前只支持png和jpeg这两种格式)
*
* @param quality
* 重新生成后的bitmap的质量
*
* @return
* 返回重新生成后的bitmap
*/
private static Bitmap codec(Bitmap src, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream os = new ByteArrayOutputStream();
src.compress(format, quality, os);
byte[] array = os.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}
读取raw资源文件中的mp3文件,然后通过音乐播放器播放:
/**
* 把mp3文件写入卡
*
* @param fileName
* 输出的文件名(全路径)
* @param context
* context对象
*/
private void writeMP3ToSDcard(String fileName, Context context) {
byte[] buffer = new byte[1024 * 8];
int read;
BufferedInputStream bin = new BufferedInputStream(context.getResources().openRawResource(R.raw.ring));
try {
BufferedOutputStream bout = new BufferedOutputStream(new FileOutputStream(fileName));
while ((read = bin.read(buffer)) > -1) {
bout.write(buffer, 0, read);
}
bout.flush();
bout.close();
bin.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(android.content.Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(newFile("XXXXmp3的文件全路径")),"audio/*");
startActivity(intent);
拦截Menu
重写public boolean onMenuOpened(int featureId, Menu menu)
返回值是false系统menu不会弹出,返回值是true系统menu会弹出,所以为了拦截Menu,返回值一定要是false
@Override
public boolean onMenuOpened(int featureId, Menu menu) {
//TODO
return false;
}
通过反射获得ITelephony对象,并调用其endCall()方法
private void invokeCallEnd() {
ITelephony iTelephony = null;
TelephonyManager telephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
try {
Method getITelephonyMethod = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[]) null);
getITelephonyMethod.setAccessible(true);
iTelephony = (ITelephony) getITelephonyMethod.invoke(telephonyMgr, (Object[]) null);
} catch (Exception e) {
e.printStackTrace();
}
try {
iTelephony.endCall();
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
ITelephony.aidl是源码中的服务,需要放在所建的项目中,包名要和ITelephony.aidl文件中的包名一致,附件是ITelephony.aidl文件
分享到:
相关推荐
1.1 课题背景………………………………………………………………………………………...1 1.2 国内外研究现状…………………………………………………………………………….....2 1.3 发展趋势…………………………...
Awesome-Android-Interview:很棒的android专家访谈问答(持续更新中……)
13 Android Code Challenge……………… 14 创建Android 应用程序…………………15 剖析一个Android 应用程序……………………… 15 Activity…………………………………………………………………15 Intent ...
Android的Handler使用方法总结,不错的文档,跟大家分享分享
android调试方法总结,总结 android 调试 的一般方法,
Android端app调用系统功能完成电池监控…………………………………………………………………………………………
。。。疑难问题,持续更新
JavaScript调用Android方法,向Android方法传递json数据。
I. What is in the Box….……………………………….... Featured Apps……………………………………………………..….….35-76 VI. Learn more about Android STB......................................77-92
android图片处理总结,总结了一些关于安装的图片处理方法,欢迎借鉴
android apk获取root权限方法总结
java和android的通用方法 下载后可在项目里直接使用 非常有用可提高程序员的开发效率
Android 常用的方法代码总结,常用代码库,复制黏贴即可用
第一章 前言 ……………………………………………………………………………………………… 2 第二章 可行性论证 …………………………………………………………………………………… 3 第三章 运行环境及开发工具...
2019年android学习方法归纳总结.pdf
主要介绍了 Android截屏截图方法汇总(Activity、View、ScrollView、ListView、RecycleView、WebView截屏截图)的相关资料,需要的朋友可以参考下
Android sdk 在首次安装好后 要用SDK Manager.exe进行更新,因服务器在国外,下载经常断开及下载非常慢 上面我把SDK的更新所需要各文件下载地址提供出来,用下载工具下载后即可使用,上面已更新到2014年2月6日 第一...
android增量更新native方法c实现部分,参考我的博客里关于增量更新的介绍
体验最新的Android...……………………………………………………………………………… 阿里云盘不限速链接分先 …………………………………………………… 先到先得 …………………………………………………………
对android基础控件的学习总结,包括谢谢使用方法及代码!