Android 从相机捕获图像,在 data.getExtras() 上抛出空指针



我使用的代码与 https://developer.android.com/training/camera/photobasics.html 我正在从相机捕获图像并将其保存到imageView。一切看起来都很好,但我正在将捆绑包作为空活动结果。我已经添加了上面链接中提到的所有权限和路径,我的代码如下

    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;
        }
    }

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

如果要传递带有相机意图的额外参数MediaStore.EXTRA_OUTPUT,则必须将fileUri保存为成员变量,以便以后可以在onActivityResult((中访问它。

希望对您有所帮助!

最新更新