我尝试将保存在手机存储空间(.jpg)上的图像转换为位图,然后将此位图转换为字节[]以将此图像上传到数据库。
public void insertData() {
File file = new File(photoUrl);
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
bytes = stream.toByteArray();
DBConnUpdate conn = new DBConnUpdate();
conn.execute( "insert into test (img, name) "+
"values ( '"+ bytes +"', '"+ productName +"')");
}
但由于此处出现OutOfMemory错误,它无法正常工作:
bytes = stream.toByteArray();
为什么?
答案 0 :(得分:0)
Try This Code:-
BitmapFactory.Options options = new BitmapFactory.Options();
options.inDither = false;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inSampleSize = 1;
options.inPurgeable = true;
options.inPreferQualityOverSpeed = true;
options.inTempStorage=new byte[32 * 1024];
File file = new File(photoUrl);
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(), options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 80, stream);
bytes = stream.toByteArray();