每当我在我的实际或虚拟 android 设备上运行此代码时,程序首先打开一个带有按钮和图像视图小工具的屏幕,按下按钮后,它会转到相机应用程序并允许我拍照。图片已保存,但之后图片应该打开并应该绘制一个矩形,但程序却关闭了。它说“压力分析已停止工作”,任何人都可以帮助我。我是android的新手,所以一个简单的解释将不胜感激。
主要活动
package com.example.stressanalysis;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
ImageView imageView;
Button button1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) this.findViewById(R.id.imageView1);
button1 = (Button) this.findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
Rect r = new Rect(0,0,50,50);
Paint paint = new Paint();
imageView.setImageBitmap(photo);
Canvas canvas = new Canvas(photo);
canvas.drawRect(r, paint);
}
}
}
主要活动 XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/ic_launcher" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="66dp"
android:text="Button" />
</RelativeLayout>
日志猫
03-12 19:19:11.380: D/AndroidRuntime(11368): Shutting down VM
03-12 19:19:11.380: W/dalvikvm(11368): threadid=1: thread exiting with uncaught exception (group=0x4107e2a0)
03-12 19:19:11.388: E/AndroidRuntime(11368): FATAL EXCEPTION: main
03-12 19:19:11.388: E/AndroidRuntime(11368): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data dat=content://media/external/images/media/5291 (has extras) }} to activity {com.example.stressanalysis/com.example.stressanalysis.MainActivity}: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.app.ActivityThread.deliverResults(ActivityThread.java:3179)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3222)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.app.ActivityThread.access$1100(ActivityThread.java:140)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1276)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.os.Looper.loop(Looper.java:137)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.app.ActivityThread.main(ActivityThread.java:4895)
03-12 19:19:11.388: E/AndroidRuntime(11368): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 19:19:11.388: E/AndroidRuntime(11368): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 19:19:11.388: E/AndroidRuntime(11368): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
03-12 19:19:11.388: E/AndroidRuntime(11368): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
03-12 19:19:11.388: E/AndroidRuntime(11368): at dalvik.system.NativeStart.main(Native Method)
03-12 19:19:11.388: E/AndroidRuntime(11368): Caused by: java.lang.IllegalStateException: Immutable bitmap passed to Canvas constructor
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.graphics.Canvas.<init>(Canvas.java:127)
03-12 19:19:11.388: E/AndroidRuntime(11368): at com.example.stressanalysis.MainActivity.onActivityResult(MainActivity.java:42)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.app.Activity.dispatchActivityResult(Activity.java:5347)
03-12 19:19:11.388: E/AndroidRuntime(11368): at android.app.ActivityThread.deliverResults(ActivityThread.java:3175)
03-12 19:19:11.388: E/AndroidRuntime(11368): ... 11 more
03-12 19:19:20.755: I/Process(11368): Sending signal. PID: 11368 SIG: 9
安慰
[2013-03-12 18:26:10 - StressAnalysis] ------------------------------
[2013-03-12 18:26:10 - StressAnalysis] Android Launch!
[2013-03-12 18:26:10 - StressAnalysis] adb is running normally.
[2013-03-12 18:26:10 - StressAnalysis] Performing com.example.stressanalysis.MainActivity activity launch
[2013-03-12 18:26:10 - StressAnalysis] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'AndroidTablet1'
[2013-03-12 18:26:10 - StressAnalysis] Uploading StressAnalysis.apk onto device 'emulator-5554'
[2013-03-12 18:26:13 - StressAnalysis] Installing StressAnalysis.apk...
[2013-03-12 18:26:18 - StressAnalysis] Success!
[2013-03-12 18:26:18 - StressAnalysis] Starting activity com.example.stressanalysis.MainActivity on device emulator-5554
[2013-03-12 18:26:20 - StressAnalysis] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.stressanalysis/.MainActivity }
[2013-03-12 18:48:23 - StressAnalysis] ------------------------------
[2013-03-12 18:48:23 - StressAnalysis] Android Launch!
[2013-03-12 18:48:23 - StressAnalysis] adb is running normally.
[2013-03-12 18:48:23 - StressAnalysis] Performing com.example.stressanalysis.MainActivity activity launch
[2013-03-12 18:48:23 - StressAnalysis] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2013-03-12 18:48:33 - StressAnalysis] Uploading StressAnalysis.apk onto device 'c1607cb047e457f'
[2013-03-12 18:48:34 - StressAnalysis] Failed to install StressAnalysis.apk on device 'c1607cb047e457f': Local path doesn't exist.
[2013-03-12 18:48:34 - StressAnalysis] com.android.ddmlib.SyncException: Local path doesn't exist.
[2013-03-12 18:48:34 - StressAnalysis] Launch canceled!
[2013-03-12 18:48:38 - StressAnalysis] ------------------------------
[2013-03-12 18:48:38 - StressAnalysis] Android Launch!
[2013-03-12 18:48:38 - StressAnalysis] adb is running normally.
[2013-03-12 18:48:38 - StressAnalysis] Performing com.example.stressanalysis.MainActivity activity launch
[2013-03-12 18:48:38 - StressAnalysis] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2013-03-12 18:48:44 - StressAnalysis] Uploading StressAnalysis.apk onto device 'c1607cb047e457f'
[2013-03-12 18:48:44 - StressAnalysis] Installing StressAnalysis.apk...
[2013-03-12 18:48:47 - StressAnalysis] Success!
[2013-03-12 18:48:47 - StressAnalysis] Starting activity com.example.stressanalysis.MainActivity on device c1607cb047e457f
[2013-03-12 18:48:48 - StressAnalysis] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.stressanalysis/.MainActivity }
[2013-03-12 18:52:54 - AndroidTest] ------------------------------
[2013-03-12 18:52:54 - AndroidTest] Android Launch!
[2013-03-12 18:52:54 - AndroidTest] adb is running normally.
[2013-03-12 18:52:54 - AndroidTest] Performing com.example.androidtest.MainActivity activity launch
[2013-03-12 18:52:57 - AndroidTest] Uploading AndroidTest.apk onto device 'c1607cb047e457f'
[2013-03-12 18:52:57 - AndroidTest] Installing AndroidTest.apk...
[2013-03-12 18:52:59 - AndroidTest] Success!
[2013-03-12 18:52:59 - AndroidTest] Starting activity com.example.androidtest.MainActivity on device c1607cb047e457f
[2013-03-12 18:53:00 - AndroidTest] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.androidtest/.MainActivity }
[2013-03-12 19:15:36 - StressAnalysis] ------------------------------
[2013-03-12 19:15:36 - StressAnalysis] Android Launch!
[2013-03-12 19:15:36 - StressAnalysis] adb is running normally.
[2013-03-12 19:15:36 - StressAnalysis] Performing com.example.stressanalysis.MainActivity activity launch
[2013-03-12 19:15:36 - StressAnalysis] Automatic Target Mode: Unable to detect device compatibility. Please select a target device.
[2013-03-12 19:15:40 - StressAnalysis] Application already deployed. No need to reinstall.
[2013-03-12 19:15:40 - StressAnalysis] Starting activity com.example.stressanalysis.MainActivity on device c1607cb047e457f
[2013-03-12 19:15:41 - StressAnalysis] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.example.stressanalysis/.MainActivity }
[2013-03-12 19:15:41 - StressAnalysis] ActivityManager: Warning: Activity not started, its current task has been brought to the front