在android中使用相机意图时获取nullpointer异常

时间:2014-06-16 09:10:01

标签: android nullpointerexception android-camera-intent onactivityresult

我已经制作了一个简单的Android应用程序,因为我想要捕获一张图片,并希望将其设置为一个imageview,但它在onActivityResult中给我空指针异常,我的logcattrace和代码如下: 的

  public void onClick(DialogInterface dialog, int item) {

                if (options[item].equals("Take Photo"))

                {

                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                    File f = new File(android.os.Environment.getExternalStorageDirectory(), "temp.jpg");

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

                    startActivityForResult(intent, 1);

                }
...
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            if (Build.VERSION.SDK_INT < 19) {
                Log.i("Your version Build.VERSION.SDK_INT < 19","if condition");//<19
                selectedImagePath = getPath(selectedImageUri);
                bitmap_selected_image2 = BitmapFactory.decodeFile(selectedImagePath);
                v_iv_photo_main.setImageBitmap(bitmap_selected_image2);
                // finish();
               //  startActivity(new Intent(SelectPhotoCountryActivity.this,SetPhotoFrameActivity.class));

            }

logcat的

06-16 14:34:15.564: E/AndroidRuntime(31301): FATAL EXCEPTION: main
06-16 14:34:15.564: E/AndroidRuntime(31301): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.footballapp/com.example.footballapp.SelectPhotoCountryActivity}: java.lang.NullPointerException
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3518)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.app.ActivityThread.handleSendResult(ActivityThread.java:3561)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.app.ActivityThread.access$1200(ActivityThread.java:168)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1377)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.os.Looper.loop(Looper.java:176)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.app.ActivityThread.main(ActivityThread.java:5493)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at java.lang.reflect.Method.invokeNative(Native Method)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at java.lang.reflect.Method.invoke(Method.java:525)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1225)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1041)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at dalvik.system.NativeStart.main(Native Method)
06-16 14:34:15.564: E/AndroidRuntime(31301): Caused by: java.lang.NullPointerException
06-16 14:34:15.564: E/AndroidRuntime(31301):    at com.example.footballapp.SelectPhotoCountryActivity.onActivityResult(SelectPhotoCountryActivity.java:281)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.app.Activity.dispatchActivityResult(Activity.java:5563)
06-16 14:34:15.564: E/AndroidRuntime(31301):    at android.app.ActivityThread.deliverResults(ActivityThread.java:3514)
06-16 14:34:15.564: E/AndroidRuntime(31301):    ... 11 more

1 个答案:

答案 0 :(得分:0)

package edu.gvsu.cis.masl.camerademo;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MyCameraActivity extends Activity {
    private static final int CAMERA_REQUEST = 1888; 
    private ImageView imageView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.imageView = (ImageView)this.findViewById(R.id.imageView1);
        Button photoButton = (Button) this.findViewById(R.id.button1);
        photoButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) { /* create an instance of intent
 * pass action android.media.action.IMAGE_CAPTURE 
 * as argument to launch camera
 */
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
/*create instance of File with name img.jpg*/
File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
/*put uri as extra in intent object*/
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
/*start activity for result pass intent as argument and request code */
startActivityForResult(intent, 1)
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);
  //if request code is same we pass as argument in startActivityForResult
  if(requestCode==1){
   //create instance of File with same name we created before to get image from storage
   File file = new File(Environment.getExternalStorageDirectory()+File.separator + "img.jpg");
   //get bitmap from path with size of
   imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(file.getAbsolutePath(), 600, 450));
   }
 }
 public static Bitmap decodeSampledBitmapFromFile(String path,
   int reqWidth, int reqHeight) { 
  // First decode with inJustDecodeBounds=true to check dimensions
  final BitmapFactory.Options options = new BitmapFactory.Options();
  //Query bitmap without allocating memory
  options.inJustDecodeBounds = true;
  //decode file from path
  BitmapFactory.decodeFile(path, options);
  // Calculate inSampleSize
  // Raw height and width of image
  final int height = options.outHeight;
  final int width = options.outWidth;
  //decode according to configuration or according best match
  options.inPreferredConfig = Bitmap.Config.RGB_565;
  int inSampleSize = 1;
  if (height > reqHeight) {
   inSampleSize = Math.round((float)height / (float)reqHeight);
  }
  int expectedWidth = width / inSampleSize;
  if (expectedWidth > reqWidth) {
   //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
   inSampleSize = Math.round((float)width / (float)reqWidth);
  }
  //if value is greater than 1,sub sample the original image
  options.inSampleSize = inSampleSize;
  // Decode bitmap with inSampleSize set
  options.inJustDecodeBounds = false;
  return BitmapFactory.decodeFile(path, options);
 }
    } 
}