First and foremost, I'm testing this app using my Google Pixel XL (1st gen 32 GB version, runnning Android 9 API Level 28)
Basically, I'm trying to make a camera application based on Android Camera 2 API (I just downloaded the sample code from https://github.com/googlesamples/android-Camera2Basic). and I'm stuck because I'm trying to save the picture taken by the camera into "Pictures" folder inside "/storage/emulated/0/
".
However, for some reason even though it seems like I'm able to do this, the folder and the picture file are not there when I checked them with ES File Explorer.
I've tried 3 methods so far:
Attempt #1:
mFile = new File(getActivity().getExternalFilesDir(Environment.DIRECTORY_PICTURES), "newPicture.jpg");
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
mFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "picture.jpg");
mFile.mkdirs();
}
Attempt #2:
File filePath = new File(Environment.getExternalStorageDirectory().getPath(),"randomFolder");
if(!filePath.exists()){
filePath.mkdirs();
}
String fullPathName = filePath.getAbsolutePath() + "/" + "newPicture.jpg";
mFile = new File(fullPathName);
Attempt #3:
mFile = new File(fullPathName);
File filePath = new File(Environment.getExternalStorageDirectory() + "/Pictures/");
if (!filePath.exists()){
filePath.mkdirs();
}
mFile = new File(filePath,"newPicture.jpg");
mFile is the File path where "newPicture" will be saved to, basically it's supposed to be saved to "/storage/emulated/0/Pictures/newPicture.jpg"
Now, I've tried to use getExternalFilesDir() method so the picture will be saved into /storage/emulated/0/Android/data/com.***.***[the package name]/files/Pictures/newPicture.jpg
" and in this case, IT TOTALLY WORKS! However I REALLY want to save the picture to /storage/emulated/0
directory because I want user to be able to access the data easily later on.
In order to illustrate this better, I've recorded the application running and then took a picture and saved it in a gif, uploaded it to imgur right here: https://i.stack.imgur.com/cNgH3.jpg
See that? The picture seems to be saved into "/storage/emulated/0/Pictures/newPicture.jpg" successfully but then when I went to ES File Explorer to check the picture, the folder "Pictures" was not created, nada.
I'm literally on my wits end right here haha, does anyone know a solution to this problem? Any advice will be greatly appreciated
P.S. Almost forgot to say, I've already added the permission in AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />