简体   繁体   中英

How get Image Uri from Camera?

Help pls, how i can get URI from camera Picture i check some articals and don't understand how dows it works, pls explain me or give some links on this topic here's my code:

    private void createDirectory() {
    directory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "Meassure Preassure Pic");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == REQUEST_CODE_PHOTO && resultCode == RESULT_OK) {
        if (intent != null && intent.getExtras() != null) {
            Bitmap imageBitmap = (Bitmap) intent.getExtras().get("data");
            ivPhoto.setImageBitmap(imageBitmap);
        }
    }
}
public void onClickPhoto(View view) {
    Intent pictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (pictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(pictureIntent, REQUEST_CODE_PHOTO);
    }
}

The onActivityResult method contains the data. Firstly you need to check if the data is null, after that you can use getdata() on returned intent to get URI. You can also get the real path of the captured image if you want to. Below is the code sample :

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_GALLERY_CODE && resultCode == Activity.RESULT_OK) {
        uri = data.getData();
        String filePath = getRealPathFromURIPath(uri, getActivity());
        File file = new File(filePath);
        Log.d(TAG, "Filename " + file.getName()); 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM