【问题标题】:App crashes after capturing image捕获图像后应用程序崩溃
【发布时间】:2018-03-23 10:49:14
【问题描述】:

在我保存时捕获相机图像后,在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);

【问题讨论】:

  • 从错误日志可以看出错误在Home.java的第101行。因此也请发布 Home.java 文件
  • 先生,我刚刚编辑了我的问题并发布了家庭代码..所以请参阅并帮助
  • data.getData() 很可能是null
  • 你检查 id data.getData()null 了吗?
  • 如何查看请告知

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


【解决方案1】:

试试这个

相机意图:

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

【讨论】:

  • 什么是 mTempCameraPhotoFile,什么是 mCurrentPhotoPath @yatin
  • 是存储在外部存储中的图片文件路径 String mCurrentPhotoPath,可以转换成uri,如Uri uri = Uri.parse(mCurrentPhotoPath);
  • 你知道你给复杂的编码是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-05-18
  • 1970-01-01
  • 2016-07-01
相关资源
最近更新 更多