Android Capture Image From Camera将空指针抛向data.getExtras()

时间:2018-01-25 07:21:04

标签: android

我使用与https://developer.android.com/training/camera/photobasics.html相同的代码   我从相机捕获图像并将其保存到imageView。一切都很好看,但我得到的Bundle as null onActivityResult。我添加了上面链接中提到的所有权限和路径,我的代码在下面

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;
    import android.provider.MediaStore;
    import android.support.v4.content.FileProvider;
    import android.util.Log;
    import android.widget.ImageView;

    import com.tcg.garageapplication.R;

    import java.io.File;
    import java.io.IOException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Date;
    public class MainActivity extends Activity {
        private static final String TAG ="ImageCaptureCamera" ;
        static final int REQUEST_TAKE_PHOTO = 1;
        ImageView ivSimpleTest;
        String mCurrentPhotoPath;
        Uri photoURI;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_image_capture_camera);
            ivSimpleTest = (ImageView)findViewById(R.id.iv_simple_test);
            dispatchTakePictureIntent();
        }
        protected void dispatchTakePictureIntent() {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                }
                if (photoFile != null) {
                   photoURI = FileProvider.getUriForFile(this,
                            "com.example.android.fileprovider",
                            photoFile);
                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                    startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
                }
            }
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
                Bundle extras = data.getExtras();
                Bitmap imageBitmap = (Bitmap) extras.get("data");
                ivSimpleTest.setImageBitmap(imageBitmap);
            }
        }
        private File createImageFile() throws IOException {
            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            String imageFileName = "JPEG_" + timeStamp + "_";
            File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
            File image = File.createTempFile(imageFileName, ".jpg", storageDir);

            mCurrentPhotoPath = image.getAbsolutePath();
            Log.e(TAG,"Value of IMAGEpATH "+mCurrentPhotoPath);
            return image;
        }
    }

1 个答案:

答案 0 :(得分:1)

具有相机意图的额外MediaStore.EXTRA_OUTPUT意图会将捕获的图像写入该路径,并且不会在onActivityResult方法中返回位图。

如果你想用摄像机意图传递额外的参数MediaStore.EXTRA_OUTPUT,你必须将fileUri保存为成员变量,以便以后可以在onActivityResult()中访问它

希望它有所帮助!