我想知道是否有可能将图像转换为base64字符串,然后使用GZIP压缩它,并通过短信将其发送到另一个android手机,然后解压缩,解码,然后图像显示给用户?如果是,那么可能的解决方案是什么?通过短信发送一个压缩的图像文件在android
-1
A
回答
0
是的,下面的代码将读取文件中的字节,gzip字节并将它们编码为base64。它适用于小于2 GB的所有可读文件。传递给Base64.encodeBytes的字节将与文件中的字节相同,因此不会丢失任何信息(与上面的代码相反,您首先将数据转换为JPEG格式)。
/*
* imagePath has changed name to path, as the file doesn't have to be an image.
*/
File file = new File(path);
long length = file.length();
BufferedInputStream bis = null;
try {
bis = new BufferedInputStream(new FileInputStream(file));
if(length > Integer.MAX_VALUE) {
throw new IOException("File must be smaller than 2 GB.");
}
byte[] data = new byte[(int)length];
//Read bytes from file
bis.read(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bis != null)
try { bis.close(); }
catch(IOException e) {
}
}
//Gzip and encode to base64
String base64Str = Base64.encodeBytes(data, Base64.GZIP);
EDIT2:这应该解码的base64字符串和解码后的数据写入文件: // outputPath是路径目标文件。
//Decode base64 String (automatically detects and decompresses gzip)
byte[] data = Base64.decode(base64str);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(outputPath);
//Write data to file
fos.write(data);
} catch(IOException e) {
e.printStackTrace();
} finally {
if(fos != null)
try { fos.close(); }
catch(IOException e) {}
}
+0
Android没有'Base64.encodeBytes'和'Base64.ZIP'选项。 (https://developer.android.com/reference/android/util/Base64.html) –
如果您在BASE64中转换图像,则无需再次压缩。您可以先压缩图像,然后在base64中转换并通过短信将其作为字符串发送给其他人 –
您不能通过* SMS *发送**除160个字符以外的任何内容。您可以通过* MMS *发送文件。但是* MMS *比SMS有更高的成本**。取决于您的操作员,大约需要5到10倍。你真的确定要使用**昂贵的服务**,而不是* eMail *,这是免费的! –
是的,我想使用短信。 –