我拍照后Android应用程序崩溃了

时间:2014-10-29 09:06:46

标签: android bitmap camera imageview

我的应用程序从FrontPageActivity开始,并有一个按钮,允许我去相机拍照。拍完照片后,我的应用程序应该将我引回到首页并向我展示图片。但是一旦拍完照片,我的应用程序崩溃了。我无法让我的模拟器拍照,所以我在手机上这样做,所以除了我的应用程序已停止,我没有收到任何错误消息。这是我的代码;

public class CameraActivity extends Activity {

private static final int CAMERA_REQUEST = 1888;
public ImageView imageView;
public static final String URI_PATH = "Uri";
Uri imageUri = null;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_frontpage);
    this.imageView = (ImageView) this.findViewById(R.id.pictureImageView);
    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");
        imageView.setImageBitmap(photo);

        Intent frontPageIntent = new Intent(this, FrontPageActivity.class);
        imageUri = data.getData();

        frontPageIntent.putExtra(URI_PATH, imageUri.toString());
        frontPageIntent.putExtra("MapPhoto", photo);

        startActivity(frontPageIntent);
    }
}
}

public class FrontPageActivity extends Activity {

private static final String NUMBER = "number";
private static final String MY_PREF = "myPref";
private static final String NA = "NA";
private ImageView imageView;
private String enteredPhoneNumber;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_frontpage);
    Bitmap bitmap = getIntent().getParcelableExtra("MapPhoto");
    if (bitmap != null) {
        imageView.setImageBitmap(bitmap);
    }

    getEnteredPhoneNumber();
    Button callButton = (Button) findViewById(R.id.callButton);
    this.imageView = (ImageView) this.findViewById(R.id.pictureImageView);

    callButton.setOnClickListener(makePhoneCall);
}

public void goToCamera(View v) {
    Intent intent = new Intent(this, CameraActivity.class);
    startActivity(intent);
}

private void getEnteredPhoneNumber() {

    SharedPreferences sharedPref = getSharedPreferences(MY_PREF,
            MODE_PRIVATE);
    enteredPhoneNumber = sharedPref.getString(NUMBER, NA);
}

private OnClickListener makePhoneCall = new OnClickListener() {

    @Override
    public void onClick(View v) {
        Intent callIntent = new Intent(Intent.ACTION_CALL);
        callIntent.setData(Uri.parse("tel:18181818," + enteredPhoneNumber));
        startActivity(callIntent);
    }
};

}

Logcat信息:

10-29 08:15:19.530: E/AndroidRuntime(907): FATAL EXCEPTION: main
10-29 08:15:19.530: E/AndroidRuntime(907): Process: com.netdesign1813, PID: 907
10-29 08:15:19.530: E/AndroidRuntime(907): java.lang.RuntimeException: Failure delivering result     ResultInfo{who=null, request=1888, result=-1, data=Intent { act=inline-data (has extras) }} to     activity {com.netdesign1813/com.netdesign1813.CameraActivity}: java.lang.NullPointerException
10-29 08:15:19.530: E/AndroidRuntime(907):  at     android.app.ActivityThread.deliverResults(ActivityThread.java:3365)
10-29 08:15:19.530: E/AndroidRuntime(907):  at a   ndroid.app.ActivityThread.handleSendResult(ActivityThread.java:3408)
10-29 08:15:19.530: E/AndroidRuntime(907):  at android.app.ActivityThread.access$1300(ActivityThread.java:135)
10-29 08:15:19.530: E/AndroidRuntime(907):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
10-29 08:15:19.530: E/AndroidRuntime(907):  at android.os.Handler.dispatchMessage(Handler.java:102)
10-29 08:15:19.530: E/AndroidRuntime(907):  at android.os.Looper.loop(Looper.java:136)
10-29 08:15:19.530: E/AndroidRuntime(907):  at android.app.ActivityThread.main(ActivityThread.java:5017)
10-29 08:15:19.530: E/AndroidRuntime(907):  at java.lang.reflect.Method.invokeNative(Native Method)
10-29 08:15:19.530: E/AndroidRuntime(907):  at java.lang.reflect.Method.invoke(Method.java:515)
10-29 08:15:19.530: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-29 08:15:19.530: E/AndroidRuntime(907):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-29 08:15:19.530: E/AndroidRuntime(907):  at dalvik.system.NativeStart.main(Native Method)
10-29 08:15:19.530: E/AndroidRuntime(907): Caused by: java.lang.NullPointerException
10-29 08:15:19.530: E/AndroidRuntime(907):  at com.netdesign1813.CameraActivity.onActivityResult(CameraActivity.java:36)
10-29 08:15:19.530: E/AndroidRuntime(907):  at android.app.Activity.dispatchActivityResult(Activity.java:5423)
10-29 08:15:19.530: E/AndroidRuntime(907):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3361)
10-29 08:15:19.530: E/AndroidRuntime(907):  ... 11 more

我希望有人能发现我的错误,因为我不能。

1 个答案:

答案 0 :(得分:0)

相机拍摄的图像尺寸可能存在问题。如果图像大小更大,则无法将图像恢复为意图中的活动。

在发出相机请求时,尝试将图像大小限制为以下内容。

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
cameraIntent.putExtra( android.provider.MediaStore.EXTRA_SIZE_LIMIT, "720000");
startActivityForResult(cameraIntent, CAMERA_REQUEST_IDPHOTO);