Nis*_*sha 0 android screenshot
我正在拍摄RelativeLayout的截图,但它会导致错误.
错误:
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)
Run Code Online (Sandbox Code Playgroud)
码:
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
你要做的是正确的.但是在onCreate()中,您的视图不会被绘制.它只是夸大了.所以你可能不得不考虑使用下面的方法.试试这个.
@Override
public void onWindowFocusChanged(boolean hasFocus)
{
super.onWindowFocusChanged(hasFocus);
share(); //Call your share() here.
}
Run Code Online (Sandbox Code Playgroud)
绘制视图后会调用此方法,因此您可以通过覆盖此方法来获取位图.
| 归档时间: |
|
| 查看次数: |
591 次 |
| 最近记录: |