应用程序在捕获图像后崩溃

时间:2018-03-23 10:49:14

标签: android android-camera android-camera-intent

在我保存后拍摄相机图像然后不在activity并且崩溃应用程序返回但是在我的三星手机中一切正常但在redmi手机和其他手机中出现此错误

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.net.Uri.toString()' on a null object reference
         at com.logiclump.technologies.gigmedico.Home.onActivityResult(Home.java:101)
         at android.app.Activity.dispatchActivityResult(Activity.java:6562)
         at android.app.ActivityThread.deliverResults(ActivityThread.java:3768)

这是我的第一个活动

 @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST_CODE && resultCode==RESULT_OK)

    {
        Uri uri = data.getData();
        Log.e("Image : ", uri.toString());
        Intent intent = new Intent(this, PictureActivity.class);
        intent.putExtra("imgUrl", uri.toString());
        startActivity(intent);
    }
}

这是我有意图的第二项活动

Bundle bundle = getIntent().getExtras();
if (bundle != null) {
    Log.e("ashish", bundle.getString("imgUrl") + "");
    path = Uri.parse(bundle.getString("imgUrl"));
}

ImageView selfiiii = (ImageView) findViewById(R.id.mySelfie);
selfiiii.setImageURI(path);

1 个答案:

答案 0 :(得分:0)

试试这个

相机意图:

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Ensure that there's a camera activity to handle the intent
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            // Error occurred while creating the File

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this,
                    "com.example.android.fileprovider",
                    photoFile);

            mTempCameraPhotoFile = photoFile;
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, PHOTO_REQUEST);
        }
    }
}

在存储中保存图像文件:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

在onActivityResult:

if (requestCode == PHOTO_REQUEST && resultCode == RESULT_OK) {
        try {

            String filePath = mTempCameraPhotoFile.getPath();

            Uri uri = Uri.fromFile(mTempCameraPhotoFile);

            Bitmap bitmap = null;

            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);

        } catch (IOException e) {
            e.printStackTrace();
        }
    }