我想在android中开发一个应用程序,在该应用程序中我需要捕获图像并将该图像转换为字符串并将该字符串写入txt文件并将其发送到服务器所在的服务器读取该文件并再次将该字符串转换为图像...如何将字符串写入txt文件并将其发送到服务器
现在我已完成图像处理并将该图像转换为字符串并将该字符串写入txt文件。
但当上午尝试读取该文件和字符串转换成它不工作的形象...
代码转换成图像串
File imageFile = new File(path);
Bitmap bitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream);
byte[] image = stream.toByteArray();
imgstr = Base64.encodeToString(image, 0);
代码编写成文件
File file = new File("new.txt");
FileWriter w = new FileWriter("/sdcard/new/new.txt");
BufferedWriter out = new BufferedWriter(w);
out.write(data);
out.flush();
out.close();
而对于读取该文件,该字符串再次转换图像的代码是
FileInputStream fstream = new FileInputStream("/sdcard/new/new.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
ArrayList list=new ArrayList();
while ((strLine = br.readLine()) != null)
{
list.add(strLine);
}
Iterator itr;
for (itr=list.iterator(); itr.hasNext();)
{
String str=itr.next().toString();
StringBuffer sb=new StringBuffer(str);
int length=sb.length();
String imageDataString = sb.substring(0, length);
byte[] decodedString = Base64.decode(imageDataString, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
FileOutputStream imageOutFile = new FileOutputStream("/sdcard/new/android.jpg");
imageOutFile.write(decodedString);
imageOutFile.close();
System.out.println("File converted");
,但它不是字符串转换为图像 请告诉我它的解决方案...
尝试输出“Base64.encode”后得到的字符串,并传递给“Base64.decode”以进行比较。如果他们太长时间比较 - 输出它们散列值并检查它们是否相等。 –
@yograjshinde究竟为什么要将图像转换为字符串发送?这是一种家庭密码学吗?该字符串的唯一优势是内容/格式操作,但这绝对不是操作图像的方式。为什么不直接上传图像? PLUS,图像数据转换为字符串可能会占用更多的空间。请满足我的好奇心,因为你当然有理由这样做。 –
为什么图像 - >字符串 - >图像?只需将它作为字节数组发送。 – m0skit0