Android 系统文件限制小探

节选自:Android 系统文件限制小探


在Linux系统中,每个进程可以使用的FD数量是有上限的,在Android中这个上限为1024,表示每个进程可以创建的file descriptors 不能超多1024个。当系统某一文件的打开句柄数超过1024时,就会报错:

"Too many open files"

修改系统最大打开文件数量:

在init.rc中增加:
setrlimit 7 1048 1048

android 版本号大小比较

节选自:android 版本号大小比较


/**
 * 版本号比较
 *
 * @param version1
 * @param version2
 * @return
 */
public static int compareVersion(String version1, String version2) {
    if (version1.equals(version2)) {
        return 0;
    }
    String[] version1Array = version1.split("\\.");
    String[] version2Array = version2.split("\\.");
    int index = 0;
    // 获取最小长度值
    int minLen = Math.min(version1Array.length, version2Array.length);
    int diff = 0;
    // 循环判断每位的大小
    while (index < minLen
            && (diff = Integer.parseInt(version1Array[index])
            - Integer.parseInt(version2Array[index])) == 0) {
        index++;
    }
    if (diff == 0) {
        // 如果位数不一致,比较多余位数
        for (int i = index; i < version1Array.length; i++) {
            if (Integer.parseInt(version1Array[i]) > 0) {
                return 1;
            }
        }

        for (int i = index; i < version2Array.length; i++) {
            if (Integer.parseInt(version2Array[i]) > 0) {
                return -1;
            }
        }
        return 0;
    } else {
        return diff > 0 ? 1 : -1;
    }
}

Android 通过apk文件获取相应的包名和版本号

节选自:Android 通过apk文件获取相应的包名和版本号


通过下面方法可以直接获取到apk的包名和版本号:apkPath是apk存放的路径

public static Drawable GetApkInfo(Context context, String apkPath) {
    PackageManager pm = context.getPackageManager();
    PackageInfo info = pm.getPackageArchiveInfo(apkPath, PackageManager.GET_ACTIVITIES);
    if (info != null) {
        ApplicationInfo appInfo = info.applicationInfo;
        String packageName = appInfo.packageName;  //得到安装包名称
        String version = info.versionName;//获取安装包的版本号
        Log.i(TAG, "getApkIcon: " + packageName + "-------" + version);
        try {
            return appInfo.loadIcon(pm);
        } catch (OutOfMemoryError e) {
            Log.i(TAG, "GetApkInfo: " + e);
        }
    }
    return null;
}

Android获取文件的MD5

节选自:Android获取文件的MD5


private void getFile(){
        String path = Environment.getExternalStorageDirectory().getAbsolutePath();
        Log.d("ATG", path);
        File file = new File(path+"/e8706cf83a2cda33dae5c40025922d75.apk");
        String md5 = getFileMD5(file);
        Log.d("ATG", md5);
    }

    public static String getFileMD5(File file) {
        if (!file.isFile()) {
            return null;
        }
        MessageDigest digest = null;
        FileInputStream in = null;
        byte buffer[] = new byte[1024];
        int len;
        try {
            digest = MessageDigest.getInstance("MD5");
            in = new FileInputStream(file);
            while ((len = in.read(buffer, 0, 1024)) != -1) {
                digest.update(buffer, 0, len);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return bytesToHexString(digest.digest());
    }

    public static String bytesToHexString(byte[] src) {
        StringBuilder stringBuilder = new StringBuilder("");
        if (src == null || src.length <= 0) {
            return null;
        }
        for (int i = 0; i < src.length; i++) {
            int v = src[i] & 0xFF;
            String hv = Integer.toHexString(v);
            if (hv.length() < 2) {
                stringBuilder.append(0);
            }
            stringBuilder.append(hv);
        }
        return stringBuilder.toString();
    }

Retrofit2文件上传与下载

节选自:Retrofit2文件上传与下载


第二步:创建用于描述网络请求的接口

public interface Api {
    /**
     * 上传
     * Multipart 这个注解代表多表单上传
     *
     * @param partList 表单信息
     * @return .
     */
    @Multipart
    @POST("服务器地址(就创建retrofit设置的基站地址后面的具体地址)")
    Call<BaseBean> upLoading(@Part List<MultipartBody.Part> partList);
}

继续阅读Retrofit2文件上传与下载

Android获取当前应用版本号

import android.content.Context;
import android.content.pm.PackageManager;

public class AppVersionUtils {
    /**
     * 获取当前app version code
     *
     * @param context
     * @return
     */
    public static int getVersionCode(Context context) {
        int versionCode = 0;
        try {
            versionCode = context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取当前app version name
     *
     * @param context 上下文
     * @return
     */
    public static String getVersionName(Context context) {
        String versionName = "";
        try {
            versionName = context.getPackageManager().
                    getPackageInfo(context.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return versionName;
    }
}