在相机意图中的onActivitystart中获取数据的空值

时间:2012-12-03 12:56:27

标签: android android-intent

OnActivityResult方法中的数据始终为null值。我尝试使用没有额外的意图,但这也不适合我。

package com.mypack;

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;

public class PhotoUploadRecommenderActivity extends Activity {

    private Button accettoButton;
    private static final int CAM_REQUEST = 1337;
    private Uri mCapturedImageURI;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.photo_upload_recommendation);

        setUpView();

    }

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

        super.onActivityResult(requestCode, resultCode, data);

          if (requestCode == CAM_REQUEST && resultCode == RESULT_OK) {
             // Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
              //imgTakenPhoto.setImageBitmap(thumbnail);

              String[] projection = { MediaStore.Images.Media.DATA}; 
              Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
              int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
              cursor.moveToFirst(); 
              String capturedImageFilePath = cursor.getString(column_index_data);

               // your bitmap
              ByteArrayOutputStream bs = new ByteArrayOutputStream();
              //thumbnail.compress(Bitmap.CompressFormat.PNG, 0, bs);


              Intent intent = new Intent(getApplicationContext(),PhotoPreviewActivity.class);
              intent.putExtra("byteArray", capturedImageFilePath);

              startActivity(intent);
          }
    }


    private void setUpView() {


        accettoButton = (Button)findViewById(R.id.accetto_button);
        accettoButton.setOnClickListener(new btnTakePhotoClicker());

    }

    class btnTakePhotoClicker implements Button.OnClickListener
    {


        public void onClick(View v) {

            String fileName = "temp.jpg";  
            ContentValues values = new ContentValues();  
            values.put(MediaStore.Images.Media.TITLE, fileName);  
            mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
            startActivityForResult(cameraIntent,CAM_REQUEST);
        }
    }

}

我想使用相机意图捕捉图像。 我收到此错误,其中还包括我的光标上的空指针异常。

java.lang.RuntimeException: Unable to resume activity {com.mypack/com.mypack.PhotoUploadRecommenderActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1337, result=-1, data=null} to activity {com.mypack/com.mypack.PhotoUploadRecommenderActivity}: java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
                   "tmp_avatar.jpg"));
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
startActivityForResult(intent, 1);

OnActivityResult:

Bundle extras = data.getExtras();
if (extras != null) {               
usrPic = extras.getParcelable("data");
    if(usrPic != null){
        imageView.setImageBitmap(usrPic);
}