Android-如何获取最近拍摄的图像的Imagepath?

Android-如何获取最近拍摄的图像的Imagepath?,android,Android,我有下面的代码捕获图像并保存它。我想在保存后立即获取最近单击的图像的路径。我怎么得到这个?我尝试了许多代码,但都不起作用。请帮忙,谢谢 public class LayoutTwoActivity extends Activity { ImageButton cambutton; Button buttons; Button button; private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE

我有下面的代码捕获图像并保存它。我想在保存后立即获取最近单击的图像的路径。我怎么得到这个?我尝试了许多代码,但都不起作用。请帮忙,谢谢

public class LayoutTwoActivity extends Activity 
{
    ImageButton cambutton;


    Button buttons;

    Button button;
    private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;

    public static final int MEDIA_TYPE_IMAGE = 1;

        // directory name to store captured images and videos
        private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
        private Uri fileUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        cambutton = (ImageButton) findViewById(R.id.imageButton3);

        text=(TextView) findViewById(R.id.textView1);
        texts = (TextView) findViewById(R.id.textView2);
        button=(Button)findViewById(R.id.button2);

        edit3= (EditText) findViewById(R.id.editText1);


        buttons=(Button)findViewById(R.id.button1);

        cambutton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // capture picture
                captureImage();
            }
        });
        if (!isDeviceSupportCamera()) {
            Toast.makeText(getApplicationContext(),
                    "Sorry! Your device doesn't support camera",
                    Toast.LENGTH_LONG).show();
            // will close the app if the device does't have camera
            finish();
        }

    public void opennewactivity(View view) {
        // Do something in response to button !! Used to call MainActivity Class on click of the image !
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }

    private void captureImage()
    {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

        // start the image capture Intent
        startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
    }

    private boolean isDeviceSupportCamera()
    {
        if (getApplicationContext().getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA)) 
        {
            // this device has a camera
            return true;
        } 
        else 
        {
            // no camera on this device
            return false;
        }
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on scren orientation
        // changes
        outState.putParcelable("file_uri", fileUri);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    {
        // if the result is capturing Image
        if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // successfully captured the image
                // display it in image view
                //previewCapturedImage();
            } else if (resultCode == RESULT_CANCELED) {
                // user cancelled Image capture
                Toast.makeText(getApplicationContext(),
                        "User cancelled image capture", Toast.LENGTH_SHORT)
                        .show();
            } else {
                // failed to capture image
                Toast.makeText(getApplicationContext(),
                        "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                        .show();
            }
        } 
    }
    public Uri getOutputMediaFileUri(int type) 
    {
        return Uri.fromFile(getOutputMediaFile(type));
    }

    /*
     * returning image / video
     */
    private static File getOutputMediaFile(int type) {

        // External sdcard location
        File mediaStorageDir = new File(
                Environment
                        .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                IMAGE_DIRECTORY_NAME);

        // Create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(IMAGE_DIRECTORY_NAME, "Oops! Failed create "
                        + IMAGE_DIRECTORY_NAME + " directory");
                return null;
            }
        }

        // Create a media file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
                Locale.getDefault()).format(new Date());
        File mediaFile;
        if (type == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +"Forest Project"
                    + "IMG_" + timeStamp + ".jpg");
        }  else {
            return null;
        }

        return mediaFile;
    }





        }
}

下面的代码将创建并设置图像的文件名

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = JPEG_FILE_PREFIX + timeStamp + "_";
    File albumF = getAlbumDir();
    File imageF = File.createTempFile(imageFileName, JPEG_FILE_SUFFIX, albumF);
    return imageF;
}

private File setUpPhotoFile() throws IOException {

    File f = createImageFile();
    mCurrentPhotoPath = f.getAbsolutePath();

    return f;
}

有关更多信息,请查看

您可以获得如下文件路径:-

   String Path = fileUri.getPath();

您的代码只创建一个图像文件。我的代码已经包含创建文件的逻辑。我只想得到拍摄后的图像路径!泰@r4jiv007谢谢你这么简单的事!这花了我几个小时才找到解决方案和许多不相关的答案,你救了我:)效果非常好:)谢谢:)