我正在截取相对布局的屏幕截图,但它会导致错误。
错误:
05-10 17:43:44.249: ERROR/AndroidRuntime(7721): Caused by: java.lang.NullPointerException
05-10 17:43:44.249: ERROR/AndroidRuntime(7721): at android.graphics.Bitmap.createBitmap(Bitmap.java:358)
05-10 17:43:44.249: ERROR/AndroidRuntime(7721): at com.API.Connect.share(FacebookConnect.java:110)
代码:
public class Connect extends Activity implements LoginListener {
public static String news = " ";
RelativeLayout bitmapimage;
Bitmap bitmap;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.image);
bitmapimage = (RelativeLayout) findViewById(R.id.bitmapimg);
TextView txtlove = (TextView) findViewById(R.id.txtlove);
txtlove.setText(news.toString());
share();
}
public void share() {
View v1 = bitmapimage;
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
saveImage();
// TODO Auto-generated method stub
}
void saveImage() {
String state = Environment.getExternalStorageState();
Environment.MEDIA_MOUNTED.equals(state);
File myDir = new File("/mnt/sdcard/DCIM/Camera");
myDir.mkdirs();
String fname = "mytry.jpg";
File file = new File(myDir, fname);
if (file.exists())
file.delete();
try {
FileOutputStream out = new FileOutputStream(file);
// create a Bitmap object from our image path.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}