android工具类常用方法

1.获取手机唯一识别号

添加AndroidManifest.xml权限

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

	public static String getIdentifyNumber(Context context){
		TelephonyManager telephonyManager=(TelephonyManager)context.getSystemService(context.TELEPHONY_SERVICE);
	    return telephonyManager.getDeviceId();
	}

2.获取当前APP的versionName

public static String getVersionName(Context context){
        String versionName="";
        try {
            String pkName=context.getPackageName();
            PackageManager pm=context.getPackageManager();
            PackageInfo pi=pm.getPackageInfo(pkName, 0);
            versionName=pi.versionName;
        } catch (NameNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return versionName;
    }

3.获取assets文件夹中的json文件

public static String getJsonString(Context context,String fileName){
        StringBuilder stringBuilder=new StringBuilder();
        try {
        AssetManager assetManager=context.getAssets();
        BufferedReader bf=new BufferedReader(new InputStreamReader(assetManager.open(fileName)));
        String line;
            while((line=bf.readLine())!=null){
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }

4.获取assets文件夹中的json文件(存在中文编码GBK)

public static String getJsonChineseString(Context context,String fileName){
        String string=new String();
        String line;
        try {
            InputStream inputStream=context.getResources().getAssets().open(fileName);
            byte[] buffer=new byte[inputStream.available()];
            inputStream.read(buffer); 
            string=new String(buffer,"GBK"); 
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        return string;
    }

5.判断当前手机的语言设置是否为中文

/**
     * 是否是中文版本
     * @param context
     * @return
     * zh 中文  en 英文
     */
    public static boolean isZH(Context context){
        Locale locale = context.getResources().getConfiguration().locale;
        String language = locale.getLanguage();
        if (language.endsWith("zh"))
            return true;
        else
            return false;
    }

6.将Bitmap文件存储为jpg

    /**
     * bitmap 存储为本地jpg,单个文件存储
     * @param bitmap bitmap图片
     * @param path  存储路径
     * @return
     */
    public static boolean saveBitmap(Bitmap bitmap, String path){
        File file=new File(path);
        if(file.exists()){
            file.delete();
        }
        FileOutputStream fo=null;
        try {
            fo=new FileOutputStream(file);
            bitmap.compress(Bitmap.CompressFormat.JPEG,100,fo);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                if(fo!=null){
                    fo.flush();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            try {
                if(fo!=null){
                    fo.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
        }
        return true;
    }

7.发现前置摄像头,camera.open(以下函数返回值)  来打开相应相机

    /***
     *  发现前端摄像头
     * @return -1未找到
     */
    @TargetApi(9)
    public int FindFrontCamera(){
        if(Build.VERSION.SDK_INT>=9){
            int cameraCount = 0;
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            cameraCount = Camera.getNumberOfCameras(); // get cameras number
            for(int camIdx=0;camIdx<cameraCount;camIdx++){
                Camera.getCameraInfo( camIdx, cameraInfo ); // get camerainfo
                if(cameraInfo.facing==Camera.CameraInfo.CAMERA_FACING_FRONT){
                    return camIdx;
                }
            }
        }
        return -1;
    }

8.发现后置摄像头

    /***
     * 发现后端摄像头,API9以上调用
     * @return -1未找到
     */
    @TargetApi(9)
    public int FindBackCamera(){
        if(Build.VERSION.SDK_INT>=9){
            int cameraCount = 0;
            Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
            cameraCount = Camera.getNumberOfCameras(); // get cameras number
            for(int camIdx=0;camIdx<cameraCount;camIdx++){
                Camera.getCameraInfo( camIdx, cameraInfo ); // get camerainfo
                if(cameraInfo.facing==Camera.CameraInfo.CAMERA_FACING_BACK){
                    return camIdx;
                }
            }
        }
        return -1;
    }
View Code

 

posted @ 2016-01-14 17:44  化身程序猿  阅读(501)  评论(0编辑  收藏  举报