private void saveImage(View view){
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + "spacitron.png";
String spacitron = "spacitron";
Bitmap bitmap = BitmapFactory.decodeByteArray(spacitron.getBytes(), 0, spacitron.getBytes().length*5);
OutputStream fout = null;
File imageFile = new File(mPath);
try {
fout = new FileOutputStream(imageFile);
//This line throws a null pointer exception
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, fout);
fout.flush();
fout.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}
}
但是,这不会创建位图,而是会引发空指针异常.如何将字符串保存到位图?
解决方法:
创建一个画布对象,并在画布上绘制文本.然后将画布另存为位图
Bitmap todisk = Bitmap.createBitmap(w,h,Bitmap.Config.ARGB_8888);
canvas.setBitmap(todisk);
canvas.drawText(message, x , y, paint);
todisk.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(new File("/mnt/sdcard/viral.jpg")));