2

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" />
blue2609
  • 841
  • 2
  • 10
  • 25

1 Answers1

2

As someone said in this post:

how-can-i-access-storage-emulated-0-dcim-on-android-device , The "/storage/emulated/" folder does not really exist. It's what might be called a "symbolic link", or, in simpler terms, a reference to where the real data is stored. You'll need to find the actual physical location on your device where it is stored.

Maybe this link will help, Or this Can't create a directory on /storage/emulated/0 on emulator.

Guy Luz
  • 3,372
  • 20
  • 43
  • I don't know man..., it's a bit weird because if I try to use the "getExternalFilesDir()" method, it'll be saved into "`/storage/emulated/0/Android/data/com.***.***[the package name]/files/Pictures/newPicture.jpg`" right? Well, the toast will show that it's saved there and when I check it with ES File explorer, "`newPicture.jpg`' was created there. Point is, I think the toast is showing the right thing, it's just not saved there for some reason. – blue2609 Jan 08 '19 at 00:34