Get file path of image on Android

后端 未结 8 1751
长发绾君心
长发绾君心 2020-11-30 09:48

I have an app that can make pictures and upload them. The upload requires the file path of the photo but I can\'t get it.

This is my code:

public voi         


        
8条回答
  •  我在风中等你
    2020-11-30 10:29

    In order to take a picture you have to determine a path where you would like the image saved and pass that as an extra in the intent, for example:

    private void capture(){
        String directoryPath = Environment.getExternalStorageDirectory() + "/" + IMAGE_DIRECTORY + "/";
        String filePath = directoryPath+Long.toHexString(System.currentTimeMillis())+".jpg";
        File directory = new File(directoryPath);
        if (!directory.exists()) {
            directory.mkdirs();
        }
        this.capturePath = filePath; // you will process the image from this path if the capture goes well
        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra( MediaStore.EXTRA_OUTPUT, Uri.fromFile( new File(filePath) ) );
        startActivityForResult(intent, REQUEST_CAPTURE);                
    
    }
    

    I just copied the above portion from another answer I gave.

    However to warn you there are a lot of inconsitencies with image capture behavior between devices that you should look out for.

    Here is an issue I ran into on some HTC devices, where it would save in the location I passed and in it's default location resulting in duplicate images on the device: Deleting a gallery image after camera intent photo taken

提交回复
热议问题