【问题标题】:camera activity gives null pointer exception相机活动给出空指针异常
【发布时间】:2012-05-21 07:47:23
【问题描述】:

在我的相机意图中:

fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

这部分给了我一个空指针异常。 谁能解释一下为什么以及需要改变什么??

button_1.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name

        startActivityForResult(intent, TAKE_PICTURE);
    }
});

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Toast.makeText(this, "Image saved to:\n" +
                     data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }
}

【问题讨论】:

  • 好吧,我自己上周刚遇到这个问题,并且被困了很长一段时间。经过很长的 long 时间,我找到了答案:stackoverflow.com/a/10613299/1056359
  • 你在哪个设备上运行这个?

标签: android android-camera


【解决方案1】:

试试下面的,

public class Camera extends Activity 
  {
 private static final int CAMERA_REQUEST = 1888;
 private String selectedImagePath;
 WebView webview;
 String fileName = "capturedImage.jpg";
 private static Uri mCapturedImageURI; 

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    Intent cameraIntent = new Intent(ACTION_IMAGE_CAPTURE); 
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_REQUEST) 
        { 
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
            Random randomGenerator = new Random();randomGenerator.nextInt();
            String newimagename=randomGenerator.toString()+".jpg";
            File f = new File(Environment.getExternalStorageDirectory()
                                    + File.separator + newimagename);
            try {
                f.createNewFile();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //write the bytes in file

            try {
                fo = new FileOutputStream(f.getAbsoluteFile());
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            try {
                fo.write(bytes.toByteArray());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
                uri=f.getAbsolutePath(); 
    //this is the url that where you are saved the image
      }



  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-04-07
    • 1970-01-01
    • 2014-09-06
    • 2014-07-19
    • 2017-01-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多