繁体   English   中英

Android拍照-java.lang.NullPointerException

[英]Android take photo - java.lang.NullPointerException

我要在“活动”中拍照,但出现此错误:

活动加载正常,当我按一下拍照时便崩溃了。

12-24 17:22:19.347: E/AndroidRuntime(32764): FATAL EXCEPTION: main
12-24 17:22:19.347: E/AndroidRuntime(32764): java.lang.NullPointerException
12-24 17:22:19.347: E/AndroidRuntime(32764):    at java.io.File.fixSlashes(File.java:185)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at java.io.File.<init>(File.java:134)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at com.example.CameraActivity$1.onPictureTaken(CameraActivity.java:287)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at android.hardware.Camera$EventHandler.handleMessage(Camera.java:855)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at android.os.Handler.dispatchMessage(Handler.java:107)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at android.os.Looper.loop(Looper.java:194)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at android.app.ActivityThread.main(ActivityThread.java:5449)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at java.lang.reflect.Method.invokeNative(Native Method)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at java.lang.reflect.Method.invoke(Method.java:525)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-24 17:22:19.347: E/AndroidRuntime(32764):    at dalvik.system.NativeStart.main(Native Method)

nnnnnnnn nnnnnnnnnnnn nnnnnnnnnnnn nnnnnnnnnnnnnnnnnn这是我的代码的公园:

private PictureCallback mPicture = new PictureCallback() {
    @Override
    public void onPictureTaken(byte[] data, Camera camera) {
        Log.e("camera test", "1111");
        File pictureFile = new File( outFile ); //********* LINE 287
        if (pictureFile == null){
            Log.d(TAG, "Error creating media file, check storage permissions: ");
            return;
        }
        /*
        imageCrop(data, pictureFile, 0, (int)(1200 / 720 * top_px), 1200, 1200);

        Intent returnIntent = new Intent();
        returnIntent.putExtra("capturedFile",pictureFile.getAbsolutePath());
        setResult(RESULT_OK,returnIntent);
        finish();
        */

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(data);
            fos.close();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            Bitmap btemp = BitmapFactory.decodeFile(outFile,options);

            Log.e("picture captured", options.outHeight + "|" + options.outWidth);

            Intent returnIntent = new Intent();
            returnIntent.putExtra("capturedFile",pictureFile.getAbsolutePath());
            setResult(RESULT_OK,returnIntent);

            finish();
        } catch (FileNotFoundException e) {
            Log.d(TAG, "File not found: " + e.getMessage());
        } catch (IOException e) {
            Log.d(TAG, "Error accessing file: " + e.getMessage());
        }

    }
};

-----------------------更新代码------------------------- ------------------------------------------

我有一个片段,称为自定义拍照活动。

意图传递给活动调用的意图不起作用。 如果我打印logcat,则可以在“片段”中看到意图。 但是活动没有收到它。

片段:

Intent intent = new Intent();           
intent.setClass(getActivity(), CameraActivity.class);   


Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

    intent.putExtra("outFile", fileUri);                    

    Log.e(TAG, "" +
            "------------------------------------------------------ " +
            "fileUri intent => " + fileUri);

startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

活动:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

Intent intent = getIntent();
outFile = intent.getStringExtra("outFile");

String test = getIntent().getExtras().getString("outFile");

Log.i("CameraActivity", "" +
        "- " +
        "test intent => " + test);


Log.e("CameraActivity", "" +
        "- " +
        "intent.getStringExtra(outFile) => " + intent.getStringExtra("outFile"));  

Log.e("CameraActivity", "" +
        "- " +
        "outFile intent => " + outFile);

logcat的:

来自fragemnt:

12-26 11:12:21.399: E/PostProductFragment(3563): - fileUri intent => file:///mnt/sdcard/Pictures/MyCameraApp/IMG_20141226_111221.jpg

从活动日志:

12-26 11:12:21.449: I/CameraActivity(3563): - test intent => null
12-26 11:12:21.449: E/CameraActivity(3563): - intent.getStringExtra(outFile) => null
12-26 11:12:21.449: E/CameraActivity(3563): - outFile intent => null

使用此代码:

    @Override
    public void onClick(View v) {

     Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
     fileUri1 = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
     cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri1);
            }
        });

返回调用此方法后:

   if (resultCode == RESULT_OK) {
        if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 4;
            Bitmap photo = (Bitmap) data.getExtras().get("data");
            ImageView imageView = (ImageView) findViewById(R.id.host_img_showlogo);
            imageView.setImageBitmap(photo);
            final Bitmap bitmap = BitmapFactory.decodeFile(fileUri1.getPath(), options);
            Image_path= fileUri1.getPath();     
        }

我使用以下代码在我的应用上拍照,效果很好,请尝试以下操作:

Intent c = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(c, FROM_CAMERA);


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

        if(requestCode == FROM_CAMERA  && resultCode == RESULT_OK && null != data )
        {
            Bundle extras = data.getExtras();
            Bitmap photo = extras.getParcelable("data");

           //Post here your code to get the path of the photo   

        }   

    }   

希望这对您有所帮助。

尝试检查

if (data != null) 

因为可能有多种原因导致数据为

更新:

intent.putExtra("outFile", fileUri);  

intent.putExtra("outFile", fileUri.toString());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM