String path="storage/sdcard0/a.jpg";
Bitmap b= BitmapFactory.decodeFile(path);
ByteArrayOutputStream out = null;
try {
out = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
byte[] imgBytes = out.toByteArray();
String b64=Base64.encodeToString(imgBytes, Base64.CRLF);
System.out.println(b64);
RandomAccessFile raf = new RandomAccessFile(mFile, "rw");
raf.seek(mFile.length());
raf.write(b64.getBytes());
raf.close();
} catch (Exception e) {
// TODO Auto-generated catch block
} finally {
try {
out.flush();
out.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
----------------------------------------------------------------------------
String sdPath=Environment.getExternalStorageDirectory().toString();
StringBuffer sb = new StringBuffer();
File file = new File(sdPath+"/base64.txt");
try {
FileInputStream fis = new FileInputStream(file);
int c;
while ((c = fis.read()) != -1) {
sb.append((char) c);
}
fis.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//--------------------转换分割线-------------------------------------
byte[] bytes = Base64.decode(sb.toString(), Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
File newFile = new File(sdPath, "new.jpg");
if (!newFile.exists()) {
try {
newFile.createNewFile();
FileOutputStream o = new FileOutputStream(newFile);
if (bitmap.compress(Bitmap.CompressFormat.JPEG, 100, o)) {
Toast.makeText(this, "转换成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "转换失败", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Android-读取本地文本,把Base64 datastring转换为图片
最新推荐文章于 2024-09-09 09:46:07 发布