在onActivityResult中使用相机时遇到问题

时间:2011-03-12 00:19:23

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

我有两个选项“选择照片”和“拍照” - 我的选择照片功能完全正常,但拍照时出现问题。主要是保存的图像在保存后显示在图像视图中。

定义我的照片位置:

public class photoActivity extends Activity {   
      private String photoPath;
      private ImageView imgView;
{...} 

我的相机听众:

bPicFromCam.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            String state = Environment.getExternalStorageState();

            if (Environment.MEDIA_MOUNTED.equals(state)) {

                long captureTime = System.currentTimeMillis();

                photoPath = Environment.getExternalStorageDirectory() + "/MYAPP" + captureTime + ".jpg";

                getPicFromCam(v);
            }
            else{

                Toast.makeText(getApplicationContext(),
                    "Sorry there is a problem accessing your SDCard, " +
                    "please select a picture from your gallery instead.", Toast.LENGTH_LONG).show();
            }

        }
    });

然后我的代码启动相机意图(注意photoPath是正确的):

    public void getPicFromCam(View view){

    System.out.println("photoPath: " + photoPath);
    //Outputs the CORRECT location!

    try{

        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        File photo = new File(photoPath);
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(photo));
        imageUri = Uri.fromFile(photo);
        startActivityForResult(Intent.createChooser(intent, "Capture Image"), CAPTURE_IMAGE);

    } catch (Exception e) {
        Toast.makeText(getApplicationContext(),
                getString(R.string.exception_message),
                Toast.LENGTH_LONG).show();
        Log.e(e.getClass().getName(), e.getMessage(), e);
    }
}

确定一切似乎都很好,直到这一点 - 拍摄照片并将图像保存到指定位置。

现在我正在尝试在我的图像视图中显示图像(然后可以在用户确认其好处后上传)。

我的onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
    case PICK_IMAGE:
        if (resultCode == Activity.RESULT_OK) {
            //THIS WORKS
        }
        break;

    case CAPTURE_IMAGE:         

        if (resultCode == RESULT_CANCELED) {
          Toast toast = Toast.makeText(this,"Canceled, no photo selected.", 1000);
          toast.show();
          return;
         }

        if (requestCode == CAPTURE_IMAGE && resultCode == RESULT_OK)
         {

            try {

                System.out.println("photoPath " + photoPath);
                //This is NULL!!!  And my problem, halp!

                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();  
                bitmapOptions.inSampleSize = 6;  
                bitmap = BitmapFactory.decodeFile(photoPath, bitmapOptions);

                imgView.setImageBitmap(bitmap);


            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), "Internal error",
                        Toast.LENGTH_LONG).show();
                Log.e(e.getClass().getName(), e.getMessage(), e);

            }

         }

        break;
    default:
    }
}

这就是我目前正在尝试的方式,但我也尝试在onActivityResult中使用以下内容:

bitmap = (Bitmap) data.getExtras().get("data");

但是每当我尝试改变上述方法时,我都会得到一个NPE。

修改

    03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): null
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391): java.lang.NullPointerException
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at com.myco.photoapp.SelectPhoto.onActivityResult(SelectPhoto.java:277)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.Activity.dispatchActivityResult(Activity.java:3890)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3511)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3115)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3143)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2684)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.os.Looper.loop(Looper.java:123)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at java.lang.reflect.Method.invokeNative(Native Method)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at java.lang.reflect.Method.invoke(Method.java:521)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-11 19:28:56.319: ERROR/java.lang.NullPointerException(16391):     at dalvik.system.NativeStart.main(Native Method)

以上NPE来自:

//below is line 277
String result = data.toURI();

                BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();  

                bitmapOptions.inSampleSize = 6;  
                bitmap = BitmapFactory.decodeFile(result, bitmapOptions);

4 个答案:

答案 0 :(得分:23)

当相机活动消失时,您的活动可能正在销毁并重新创建。也许尝试将photoPath保存到onSaveInstanceState中的Bundle中,然后在onCreate中将其捕获回来(确保在执行此操作时检查onCreate中的空值)?

答案 1 :(得分:4)

我无法使用这些数据。但我认为你需要改变如下

照片成为一般变量。

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

所以,你必须从这个文件夹中取出它:

Bitmap photo = Media.getBitmap(getContentResolver(), Uri.fromFile(photo) );

我按照您的代码进行更改。

答案 2 :(得分:1)

我在解决这个问题时遇到了另一个问题: 只需使用这样的代码:

Bitmap bitmap = data.getExtras().getParcelable("data");

你可以得到位图。

答案 3 :(得分:1)

基本上,使用

intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));

运作良好

在放入imageView之前,请确保压缩图像,因为以这种方式保存时图像非常大。

int newWidth = bm.getWidth();
int newHeight = bm.getHeight();
while(newWidth > 300){
    newWidth = newWidth/2;
newHeight = newHeight/2;
}
mImagePlaceHolder.setImageBitmap(Bitmap.createScaledBitmap(bm, newWidth, newHeight, false));