六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 62|回复: 0

android一些有用的方法总结(持续更新……)

[复制链接]

升级  12.67%

17

主题

17

主题

17

主题

秀才

Rank: 2

积分
69
 楼主| 发表于 2013-1-15 02:21:08 | 显示全部楼层 |阅读模式
这些方法在开发中用的上,在这里记录一下,以后查阅。
     转载请注明出处: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转换成Bytestatic 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 blocke.printStackTrace();}}ITelephony.aidl是源码中的服务,需要放在所建的项目中,包名要和ITelephony.aidl文件中的包名一致,附件是ITelephony.aidl文件
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表