在Android开发中为Camera Activity

时间:2018-03-27 21:18:11

标签: android

我正在尝试捕捉图像并转到图像的下一个意图。以下代码转到Camera Intent。但不是第二个意图。它返回java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity错误。这意味着没有数据转到onResultActivity。如果我在参数中放入数据!= null,那么它将返回到第1个Activity。我错过了什么?我测试了mCurrentPhotoPath。它返回filePath。

    String shopName;
    double latitude, longitude;
    private int REQUEST_IMAGE_CAPTURE = 0;
    private Bitmap imageBitmap;
    String mCurrentPhotoPath;
    static final int REQUEST_TAKE_PHOTO = 0;

    private File createImageFile() throws IOException {
        // Create an image file name
        @SuppressLint("SimpleDateFormat") 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;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button btnNext = (Button)findViewById(R.id.btnNext);

        btnNext.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                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
                    }

                    if (photoFile != null) {
                        //Toast.makeText(getApplicationContext(), "OK", Toast.LENGTH_LONG).show();
                        Uri photoURI = FileProvider.getUriForFile(MainActivity.this,
                                "com.marketaccess.data.collector.fileprovider",
                                photoFile);
                        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                    }
                }

            }
        });
    }

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

        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode != RESULT_CANCELED) {

            Uri filePath = data.getData();
            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
            } catch (IOException e) {
                e.printStackTrace();
            }
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 50, stream);
            byte[] imageResource  = stream.toByteArray();

            Intent sendDataIntent = new Intent(MainActivity.this, SendData.class);
            Bundle extras = new Bundle();
            sendDataIntent.putExtras(extras);
            sendDataIntent.putExtra("imageResource", imageResource);
            startActivity(sendDataIntent);
        } 
    }

我测试了Manifest fileprover块。看起来好像和退路。

0 个答案:

没有答案