如何将图像数据作为 64 进制字符串写入文本文件?
我试图将图像数据作为基本 64 字符串写入文本文件,如果它不包含太多信息,那么它可以工作,但是当它包含相当多的信息时,它会崩溃应用程序,说它的内存不足,下面是我的代码:
package com.search.visual;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;
public class SavePhotoTask extends AsyncTask<byte[], String, String>{
String name;
byte[] num;
@Override
protected String doInBackground(byte[]... params) {
// TODO Auto-generated method stub
File photo = new File(Environment.getExternalStorageDirectory(),".vis/image.xml");
name = Base64.encodeToString(params[0], 0);
//return base64String;
if (photo.exists()){
photo.delete();
}
try{
FileWriter fi = new FileWriter(photo.getPath());
fi.write("<Image>\n\t<Data>");
fi.write(name);
fi.write("</Data>\n</Image>");
fi.close();
}catch (IOException e) {
// TODO: handle exception
Log.e("pictureDemo","exception", e);
}
return (null);
}
}
我是吗做错了什么或者有更好的方法吗?
先感谢您
im trying to write image data to a text file as a base 64 string its working if it doesnt contain to much information but when it contains quite a bit of information it crashes the application saying that its out off memory below is my code:
package com.search.visual;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Base64;
import android.util.Log;
import android.widget.Toast;
public class SavePhotoTask extends AsyncTask<byte[], String, String>{
String name;
byte[] num;
@Override
protected String doInBackground(byte[]... params) {
// TODO Auto-generated method stub
File photo = new File(Environment.getExternalStorageDirectory(),".vis/image.xml");
name = Base64.encodeToString(params[0], 0);
//return base64String;
if (photo.exists()){
photo.delete();
}
try{
FileWriter fi = new FileWriter(photo.getPath());
fi.write("<Image>\n\t<Data>");
fi.write(name);
fi.write("</Data>\n</Image>");
fi.close();
}catch (IOException e) {
// TODO: handle exception
Log.e("pictureDemo","exception", e);
}
return (null);
}
}
am i doing something wrong or is there a better way to do this?
thank you in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来你的内存已经用完了。发生这种情况的原因之一是,您同时将整个原始图像和整个 Base-64 编码图像保存在内存中,即使只是短暂保存。尝试在 for 循环中一次将数据写出一个块,使用以下变体:
一次编码并写入几千个字节
Sounds like you're simply running out of RAM. One reason this is happening is because you're holding the entire original image and the entire base-64 encoded image in memory, all at once, even if only briefly. Try writing the data out a chunk at a time in a for loop, using the variation:
And encoding and writing a few thousand bytes at a time