Search code examples
androidfileandroid-camerauriandroid-camera-intent

Taking camera picture works on emulator but not on real device


I'm trying to take a picture and then save it and upload it to Firebase but for some reason when i try this on ANY emulator it works but on my real phone i get the message the file doesn't exist. NOTE: User has already given permission for camera and storage. User.pictureTaken AND User.pictureTakenName are just 2 static strings in another class that i use to just quickly save the path and the UUID.

Taking the picture:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

File file = getOutputMediaFile(null);
Uri photoUri = FileProvider.getUriForFile(getActivity(), getActivity().getApplicationContext().getPackageName() + ".provider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
User.pictureTaken = file.getPath();
Log.d("MyTag", "URL: " + file.getPath());

startActivityForResult(intent, TAKE_PHOTO_CODE);

Getting the imagepath:

private File getOutputMediaFile(String newUUID){
    if (newUUID == null) {
        newUUID = UUID.randomUUID().toString();
        User.pictureTakenName = newUUID;
    }

    return new File(getActivity().getFilesDir(), newUUID);
}

Trying to open the file:

File mediaStorageDir = getOutputMediaFile(User.pictureTakenName);
InputStream stream = new FileInputStream(mediaStorageDir);
UploadTask uploadTask =  FirebaseReferences.selfies.child(User.pictureTakenName).putStream(stream);

It's on the last line here that it gives the error, the code below that is not included as it's not relevant.


Solution

  • You gave the file path for internal storage, so try to give external storage path, it will work fine.

    Change you getOutputMediaFile like this,

    private File getOutputMediaFile(String newUUID){
        if (newUUID == null) {
            newUUID = UUID.randomUUID().toString();
            User.pictureTakenName = newUUID;
        }
    
        return new File(Environment.getExternalStorageDirectory(), newUUID);
    }
    

    And try to get back from this path.