最近在Android上捕获的文件事件拦截

时间:2015-08-04 12:46:36

标签: java android android-intent android-activity android-camera

我想做的只是添加最近捕获的文件名的日志行。不幸的是,由于以下指示错误,应用程序失败下面提供的堆栈代码和错误。

提前致谢,

的Anar

  

java.lang.RuntimeException:传递结果失败   ResultInfo {who = null,request = 100,result = -1,data = null}到活动   {az.justx.justx / az.justx.justx.MainActivity}:   java.lang.NullPointerException:尝试调用虚方法   空对象上的'android.net.Uri android.content.Intent.getData()'   参考

private void interceptCameraButtonClick() {

    Button cameraButton = (Button) findViewById(R.id.cam);

    cameraButton.setOnClickListener(
            new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent imageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
                    imagesFolder.mkdirs(); // <----
                    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                    File image = new File(imagesFolder, "AD_" + timeStamp + ".jpg");
                    Uri uriSavedImage = Uri.fromFile(image);
                    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
                    startActivityForResult(imageIntent, 100);
                }
            }
    );

}

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

    Log.i("Test", Integer.toString(resultCode));

    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
        if (resultCode == RESULT_OK) {
            // Image captured and saved to fileUri specified in the Intent
            Log.i("CALLED", "Image saved to:\n" +
                    data.getData());
            Toast.makeText(this, "Image saved to:\n" +
                    data.getData(), Toast.LENGTH_LONG).show();
        } else if (resultCode == RESULT_CANCELED) {
            // User cancelled the image capture
        } else {
            // Image capture failed, advise user
        }
    }

}

1 个答案:

答案 0 :(得分:1)

相机不要求将[{1}}中的Uri发送回Intent。您已经知道onActivityResult()是什么,正如您在Uri中所说的那样。你只需要使用那个值。请确保将其包含在已保存的实例状态中,因为您的进程可能会在相机应用程序位于前台时终止。