使用标准的Android应用程序拍照并存储在db中

时间:2012-10-24 07:03:20

标签: android camera android-camera android-camera-intent

在我的Android应用程序中,我需要调用标准照片应用程序,拍照并将其存储在设备的sqlite数据库中。

这是我启动意图按钮的处理程序

private Uri fileUri;
private File file;
private ImageView foto;

public void fotoHandler(View view) {
    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    file = FileUtilities.getOutputMediaFile();
    fileUri = Uri.fromFile(file); // create a file to save the image
    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

    // start the image capture Intent
    startActivityForResult(intent, TAKE_PICTURE);
}

这是resulthandler

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        // Image captured and saved to fileUri specified in the Intent
        Toast.makeText(this, "Image saved to:\n" +
                         fileUri.toString(), Toast.LENGTH_LONG).show();

         try {
             Bitmap bitmap =  MediaStore.Images.Media.getBitmap(this.getContentResolver(), fileUri);
             file.delete();
             foto.setImageBitmap(laatsteFoto);
         } catch (FileNotFoundException e) {
             Toast.makeText(this, "File " + fileUri.toString() + " can't be found !", Toast.LENGTH_LONG).show();
         } catch (IOException e) {
             Toast.makeText(this, "File " + fileUri.toString() + " can't be read !", Toast.LENGTH_LONG).show();
         }
     } else if (resultCode == RESULT_CANCELED) {
         // User cancelled the image capture
     } else 
         Toast.makeText(this, "Error during retrieving photo from camera !", Toast.LENGTH_LONG).show();
     }
}

它在带有ICS的Acer A100上工作正常,但在带有ICS的三星Galaxy S2上出现了问题。

10-24 08:44:05.190: E/AndroidRuntime(19861): FATAL EXCEPTION: main
10-24 08:44:05.190: E/AndroidRuntime(19861): java.lang.RuntimeException: Unable to resume activity {be.lengo.autikoken/be.lengo.autikoken.IngredientWijzigen}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {be.lengo.autikoken/be.lengo.autikoken.IngredientWijzigen}: java.lang.NullPointerException
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2455)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2483)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3362)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.access$700(ActivityThread.java:127)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.os.Looper.loop(Looper.java:137)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.main(ActivityThread.java:4511)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at dalvik.system.NativeStart.main(Native Method)
10-24 08:44:05.190: E/AndroidRuntime(19861): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {be.lengo.autikoken/be.lengo.autikoken.IngredientWijzigen}: java.lang.NullPointerException
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2991)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2442)
10-24 08:44:05.190: E/AndroidRuntime(19861):    ... 13 more
10-24 08:44:05.190: E/AndroidRuntime(19861): Caused by: java.lang.NullPointerException
10-24 08:44:05.190: E/AndroidRuntime(19861):    at be.lengo.autikoken.IngredientWijzigen.onActivityResult(IngredientWijzigen.java:233)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.Activity.dispatchActivityResult(Activity.java:4654)
10-24 08:44:05.190: E/AndroidRuntime(19861):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
10-24 08:44:05.190: E/AndroidRuntime(19861):    ... 14 more

当我用结果评论吐司时记录

10-24 09:27:06.520: E/AndroidRuntime(21849): FATAL EXCEPTION: main
10-24 09:27:06.520: E/AndroidRuntime(21849): java.lang.RuntimeException: Unable to resume activity {be.lengo.autikoken/be.lengo.autikoken.IngredientWijzigen}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {be.lengo.autikoken/be.lengo.autikoken.IngredientWijzigen}: java.lang.NullPointerException
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2455)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2483)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1997)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3362)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.access$700(ActivityThread.java:127)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1162)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.os.Looper.loop(Looper.java:137)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.main(ActivityThread.java:4511)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at dalvik.system.NativeStart.main(Native Method)
10-24 09:27:06.520: E/AndroidRuntime(21849): Caused by: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {be.lengo.autikoken/be.lengo.autikoken.IngredientWijzigen}: java.lang.NullPointerException
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2991)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2442)
10-24 09:27:06.520: E/AndroidRuntime(21849):    ... 13 more
10-24 09:27:06.520: E/AndroidRuntime(21849): Caused by: java.lang.NullPointerException
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.content.ContentResolver.openInputStream(ContentResolver.java:361)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.provider.MediaStore$Images$Media.getBitmap(MediaStore.java:715)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at be.lengo.autikoken.IngredientWijzigen.onActivityResult(IngredientWijzigen.java:236)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.Activity.dispatchActivityResult(Activity.java:4654)
10-24 09:27:06.520: E/AndroidRuntime(21849):    at android.app.ActivityThread.deliverResults(ActivityThread.java:2987)
10-24 09:27:06.520: E/AndroidRuntime(21849):    ... 14 more

2 个答案:

答案 0 :(得分:3)

如果您的活动仅在纵向模式下运行,则三星Galaxy S2上存在错误。发生的事情是你启动相机,它拍摄照片,并在返回时,首先在风景中开始你的活动,而不是在1秒内,它旋转到肖像。确保使用onSaveInstanceState正确保存活动中的所有变量并在onCreate中恢复。这件事发生在我身上,因为我没有保存它们,那电话正在旋转活动。

答案 1 :(得分:0)

fileUri可能为null。尝试正确初始化它。