无法将捕获的图像显示到其他活动,如何使其工作?

时间:2015-06-23 16:59:57

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

我想从自定义相机中捕获图像,然后将其显示在我可以添加其他png表情符号的其他活动上,我有这个代码但是在活动启动后它没有显示任何照片

1 个答案:

答案 0 :(得分:0)

请参阅我在您的代码中所做的一些更改。

1。请在拍照后调用新的Intent并像我一样传递文件路径。

MainActivity:

  ImageView mCaptureButton = (ImageView) findViewById(R.id.button_capture);
        mCaptureButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // get an image from the came
                        mCamPreview.captureImage(MainActivity.this);
    //starting(); // don't call Intent here
            }       
        });

    }

CameraPreview类:

private Activity _activity ;
    public void captureImage(Activity activity){
this._activity = activity;
    mCamera.takePicture(null, null, mJpegCallback);
}

PictureCallback mJpegCallback = new PictureCallback() {
    @Override
     public void onPictureTaken(byte[] data, Camera camera) {
        final Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);

        // Store bitmap to local storage
        FileOutputStream out = null;
        try {
            // Prepare file path to store bitmap
            // This will create Pictures/MY_APP_NAME_DIR/
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), "AVORI");
            if (!mediaStorageDir.exists()) {
                if (!mediaStorageDir.mkdirs()) {
                    Log.d(TAG, "failed to create directory");
                    return;
                }
            }

            // Bitmap will be stored at /Pictures/MY_APP_NAME_DIR/YOUR_FILE_NAME.jpg
            String filePath = mediaStorageDir.getPath() + File.separator + "Av.jpg";
            out = new FileOutputStream(filePath);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
//call EditPicture.class here
 Intent i = new Intent(_activity, EditPicture.class);
    i.putExtra("FILE_PATH", filePath );
startActivity(i); 
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
};