从相机拍照并将URI传递给另一个活动

时间:2018-12-30 00:10:15

标签: java android

当我尝试在onActivityResult()方法上获取URI时,出现了空指针异常。 我需要从设备相机拍照,然后将URI传递到下一个活动。 那是我的实际代码:

public void takePicFromCamera(View view){
    if (Build.VERSION.SDK_INT >= 24) {
        try {
            Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
            m.invoke(null);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);


    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "IMG_FOLDER");


    imageUri = Uri.fromFile(new File(mediaStorageDir.getPath() + File.separator +
            "profile_img.jpg"));
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

    startActivityForResult(intent, 2);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == 1 && resultCode != 0) {
        Uri sel_img = data.getData();
        Intent intent = new Intent(MainActivity.this, RateActivity.class);
        intent.putExtra("uri", sel_img.toString());
        startActivity(intent);

    }
    if(requestCode == 2 && resultCode != 0){

        Intent intent = new Intent(MainActivity.this, RateActivity.class);
        intent.putExtra("uri", imageUri.toString());
        startActivity(intent);
    }


}

我拍摄图像后的RateActivity无法启动

0 个答案:

没有答案