Java 图像uri没有';在我第一次调用应用程序中的相机后,不会更新

Java 图像uri没有';在我第一次调用应用程序中的相机后,不会更新,java,android,camera,uri,photo,Java,Android,Camera,Uri,Photo,当我想在我抓拍一张照片后获取图像uri时,它总是什么也得不到,但在我再次抓拍后,它返回第一个uri,而不是当前uri。看起来它总是在最新的之前得到一个。以下是我的camerapreview代码: public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback { private static final String TAG = "CameraPreview"; private Su

当我想在我抓拍一张照片后获取图像uri时,它总是什么也得不到,但在我再次抓拍后,它返回第一个uri,而不是当前uri。看起来它总是在最新的之前得到一个。以下是我的camerapreview代码:

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "CameraPreview";
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public static final int MEDIA_TYPE_IMAGE = 1;
    private Uri outputMediaFileUri;
    private String outputMediaFileType;
    public File pic;
    public Uri uri;


    public CameraPreview(Context context) {
        super(context);
        mHolder = getHolder();
        mHolder.addCallback(this);
    }

    private static Camera getCameraInstance() {
        Camera c = null;
        try {
            c = Camera.open();
        } catch (Exception e) {
            Log.d(TAG, "camera is not available");
        }
        return c;
    }

    public void surfaceCreated(SurfaceHolder holder) {
        mCamera = getCameraInstance();
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        mHolder.removeCallback(this);
        mCamera.setPreviewCallback(null);
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        int rotation = getDisplayOrientation();
        mCamera.setDisplayOrientation(90);
    }




    private File getOutputMediaFile(int type) {
        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_PICTURES), TAG);
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(TAG, "failed to create directory");
                return null;
            }
        }
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                    "prescription" + ".jpg");
            outputMediaFileType = "image/*";
        } else {
            return null;
        }
        outputMediaFileUri = Uri.fromFile(mediaFile);
        return mediaFile;
    }

    public Uri getOutputMediaFileUri() {
        return outputMediaFileUri;
    }

    public String getOutputMediaFileType() {
        return outputMediaFileType;
    }


    public void takePicture() {
        mCamera.takePicture(null, null, new Camera.PictureCallback() {
            @Override
            public void onPictureTaken(byte[] data, Camera camera) {
                File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
                Log.i("camera", "file done");
                if (pictureFile == null) {
                    Log.d(TAG, "Error creating media file, check storage permissions");
                    return;
                }
                try {
                    Log.i("camera", "do outputstream");
                    FileOutputStream fos = new FileOutputStream(pictureFile);
                    Log.i("camera", "after outputstream");
                    s = getOutputMediaFileUri().toString();
                    fos.write(data);
                    fos.close();



                } catch (FileNotFoundException e) {
                    Log.d(TAG, "File not found: " + e.getMessage());
                } catch (IOException e) {
                    Log.d(TAG, "Error accessing file: " + e.getMessage());
                }
            }
        });

    }



    public int getDisplayOrientation() {
        Display display = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
        int rotation = display.getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        android.hardware.Camera.CameraInfo camInfo =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, camInfo);

        int result = (camInfo.orientation - degrees + 360) % 360;
        return result;
    }

}
在调用camerapreview.takepicture()之后,我尝试调用camerapreview.outputMediaFileUri.toString(),但它不起作用。有人能帮我吗

更新: 我发现在主要活动中,mPreview.takePicture()总是在所有过程结束时运行:

Button buttonCapturePhoto = (Button) findViewById(R.id.button_capture_photo);

        buttonCapturePhoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPreview.takePicture();
                path = mPreview.geturi;
                goToFourthActivity(path);
             }
            }
        });
每次,“path”将首先读取图像uri,然后执行mPreview.takePicture()。所以它将始终是以前的uri,我不知道为什么,如何修复它?

您的代码

mediaFile = new File(mediaStorageDir.getPath() + File.separator +
                "prescription" + ".jpg");

无条件地重用相同的文件名。难怪您会得到相同的URI。

我认为这不是问题所在,我已经更新了导致问题的内容。当然,更新的代码不好。它做什么,就开始捕获,并设置路径。相机将在很久以后调用PictureTaken()。但是,您可能不想重复使用相同的固定文件名。因此,如果我想获得更新的uri,我应该怎么做?既然您说onPictureTaken()将运行得晚很多,我该如何修复它?感谢您提醒我重复的文件名。您可能应该从
onPictureTaken()
回调中
gotoForHactivity(path)