Android 中Base64的操作

简介: /** * <p>将文件转成base64 字符串</p> * @param path 文件路径 * @return * @throws Exception */ public static String encodeBase64File(String path) throws Exception { File file = new F
	/**
	 * <p>将文件转成base64 字符串</p>
	 * @param path 文件路径
	 * @return
	 * @throws Exception
	 */
	public static String encodeBase64File(String path) throws Exception {
		File  file = new File(path);
		FileInputStream inputFile = new FileInputStream(file);
		byte[] buffer = new byte[(int)file.length()];
		inputFile.read(buffer);
        inputFile.close();
//        return new android.util.Base64;
        MyUtils.myLog("------------", file+"===="+ "");
        return android.util.Base64.encodeToString(buffer, Base64.DEFAULT);
	}
	/**
	 * <p>将base64字符解码保存文件</p>
	 * @param base64Code
	 * @param targetPath
	 * @throws Exception
	 */
	public static void decoderBase64File(String base64Code,String targetPath) throws Exception {
		byte [] baseByte = android.util.Base64.decode(base64Code, Base64.DEFAULT);
		
//		byte[] buffer = new BASE64Decoder().decodeBuffer(base64Code);
		FileOutputStream out = new FileOutputStream(targetPath);
		out.write(baseByte);
		out.close();
	}
	/**
	 * <p>将base64字符保存文本文件</p>
	 * @param base64Code
	 * @param targetPath
	 * @throws Exception
	 */
	public static void toFile(String base64Code,String targetPath) throws Exception {
		byte[] buffer = base64Code.getBytes();
		FileOutputStream out = new FileOutputStream(targetPath);
		out.write(buffer);
		out.close();
	}
	public static void main(String[] args) {
		try {
			String base64Code =encodeBase64File("D:\\1.jpg");
			System.out.println(base64Code);
			decoderBase64File(base64Code, "D:\\2.jpg");
			toFile(base64Code, "D:\\three.txt");			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

目录
相关文章
|
2月前
|
移动开发 算法 安全
安卓逆向 -- 算法基础(Base64与HEX)
安卓逆向 -- 算法基础(Base64与HEX)
14 1
|
8月前
|
数据库 Android开发 数据库管理
Android使用Room操作SQLite数据库让其变得无比高效和简洁(进一步完善用RecyclerView显示数据库中的数据)
Android使用Room操作SQLite数据库让其变得无比高效和简洁(进一步完善用RecyclerView显示数据库中的数据)
52 0
|
4月前
|
Android开发
[Android]视图的控触操作-MotionEvent
[Android]视图的控触操作-MotionEvent
31 0
|
4月前
|
算法 Java 数据安全/隐私保护
Android App开发之利用JNI实现加密和解密操作实战(附源码 简单易懂)
Android App开发之利用JNI实现加密和解密操作实战(附源码 简单易懂)
76 0
|
8月前
|
数据库 Android开发 数据库管理
Android 使用Room操作数据库进行数据库版本的升级和迁移
Android 使用Room操作数据库进行数据库版本的升级和迁移
427 0
|
8月前
|
存储 数据库 Android开发
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(前一篇文章的完善)
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(前一篇文章的完善)
117 0
|
8月前
|
数据库 Android开发 数据库管理
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(教程一)
Android 使用Room操作SQLite数据库让其变得无比高效和简洁(教程一)
138 0
|
8月前
|
Java Android开发
Android 四大组件之ContentProvider 访问通讯录进行增删改查操作
Android 四大组件之ContentProvider 访问通讯录进行增删改查操作
51 0
|
8月前
|
API 数据库 Android开发
Android SQLite数据库中基础的增删改查操作以及API的详解
Android SQLite数据库中基础的增删改查操作以及API的详解
66 0
|
8月前
|
算法 安全 Java
Android 逆向 | 不是加密的 Base64
Android 逆向 | 不是加密的 Base64