我想在 android 上捕获一个布局文件图像并保存到 sd 卡。在这里,如果我使用此代码,我想要捕获布局 RelativeLayout,我的应用程序运行得很好。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/ChoosePictureButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Sign" />
<Button
android:id="@+id/Clear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Clear" />
<Button
android:id="@+id/SavePictureButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
</TableRow>
</TableLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="870dp"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:src="@drawable/sample" />
</FrameLayout>
<com.example.testsign.SignatureView
android:id="@+id/sign"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_alignParentBottom="true"
android:visibility="invisible" >
</com.example.testsign.SignatureView>
但是当我想捕获 Framelayout 时,我的应用程序出错了。我不明白为什么?你能帮帮我吗!
源代码:
public class Test extends Activity implements OnClickListener {
private SignatureView sign;
private Button choosePicture;
private Button savePicture;
private Button clear;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
int currentOrientation = getResources().getConfiguration().orientation;
if (currentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
else {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
}
choosePicture = (Button) this.findViewById(R.id.ChoosePictureButton);
savePicture = (Button) this.findViewById(R.id.SavePictureButton);
savePicture.setOnClickListener(this);
choosePicture.setOnClickListener(this);
clear=(Button)findViewById(R.id.Clear);
clear.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_test, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0==choosePicture)
{
sign = (SignatureView) findViewById(R.id.sign);
sign.setBackgroundColor(0xFFFFFFFF);
sign.setVisibility(View.VISIBLE);
}else if(arg0==clear)
{
//sign.setVisibility(View.INVISIBLE);
sign.clearSignature();
}else if(arg0==savePicture)
{
Toast.makeText(this, "Saved!!!Thank you!!!", Toast.LENGTH_LONG).show();
captureScreen();
}
}
private void captureScreen() {
RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.main);
Bitmap b = Bitmap.createBitmap(mainLayout.getWidth(), mainLayout
.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(b);
mainLayout.draw(c);
ContentValues contentValues = new ContentValues(3);
contentValues.put(Media.DISPLAY_NAME, "Draw On Me");
Uri imageFileUri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, contentValues);
try {
OutputStream imageFileOS = getContentResolver().openOutputStream(imageFileUri);
b.compress(CompressFormat.JPEG, 90, imageFileOS);
Toast t = Toast.makeText(this, "Thank you! Image Saved!", Toast.LENGTH_LONG);
t.show();
} catch (Exception e) {
Log.v("EXCEPTION", e.getMessage());
}
}
}
当我想要捕获帧布局时记录错误
> 11-15 09:00:30.492: W/dalvikvm(4364): threadid=1: thread exiting with uncaught exception (group=0x40bda1f8)
11-15 09:00:30.492: E/AndroidRuntime(4364): FATAL EXCEPTION: main
11-15 09:00:30.492: E/AndroidRuntime(4364): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testsign/com.example.testsign.Test}: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.Button
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.access$600(ActivityThread.java:128)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.os.Handler.dispatchMessage(Handler.java:99)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.os.Looper.loop(Looper.java:137)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.main(ActivityThread.java:4514)
11-15 09:00:30.492: E/AndroidRuntime(4364): at java.lang.reflect.Method.invokeNative(Native Method)
11-15 09:00:30.492: E/AndroidRuntime(4364): at java.lang.reflect.Method.invoke(Method.java:511)
11-15 09:00:30.492: E/AndroidRuntime(4364): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
11-15 09:00:30.492: E/AndroidRuntime(4364): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
11-15 09:00:30.492: E/AndroidRuntime(4364): at dalvik.system.NativeStart.main(Native Method)
11-15 09:00:30.492: E/AndroidRuntime(4364): Caused by: java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.Button
11-15 09:00:30.492: E/AndroidRuntime(4364): at com.example.testsign.Test.onCreate(Test.java:43)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.Activity.performCreate(Activity.java:4465)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1053)
11-15 09:00:30.492: E/AndroidRuntime(4364): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1934)
11-15 09:00:30.492: E/AndroidRuntime(4364): ... 11 more