我正在尝试将线性布局转换为位图。但它给出了一个空指针异常。我的代码是。
位图为空。因为我没有得到“不为空”的吐司。为什么我将位图设为空。我尝试了许多类似的问题,但那里的代码相同。
public class Receipt extends Activity {
public Bitmap bitmap;
LinearLayout layout;
private String fileName;
private File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.receipt);
ImageView iv = (ImageView) findViewById(R.id.Ivsignature);
iv.setImageBitmap(Signature.sign);
layout = (LinearLayout) findViewById(R.id.Llreceipt);
layout.setDrawingCacheEnabled(true);
layout.buildDrawingCache();
bitmap = layout.getDrawingCache(true);
if (bitmap != null)
Toast.makeText(getApplicationContext(), "not null",
Toast.LENGTH_LONG).show();
String myPath = Environment.getExternalStorageDirectory()
+ "/signature_image";
File myDir = new File(myPath);
try {
myDir.mkdirs();
} catch (Exception e) {
}
String fname = fileName + "gwg" + ".jpg";
file = new File(myDir, fname);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(file);
bitmap.compress(CompressFormat.JPEG, 100, fos);
Toast.makeText(getApplicationContext(), "File saved",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), String.valueOf(e),
Toast.LENGTH_LONG).show();
}
}
}