0

I'm writing a simple gallery on android using an emulator and right now I'm trying to implement functionality which allows taking a photo right from the app.

I'm saving photo right into /storage/emulated/0/DCIM/Camera/, but the problem is that no application can access newly created photos. I mean that I don't see these photos in Google photos, in the built-in gallery and of course my application.

But I can find these photos using built-in file explorer.

There is a bunch of created photos in /storage/emulated/0/DCIM/Camera/

enter image description here

But, for example, if I create a photo using standart built-in app camera (I've created 3 of them) Google photo sees only them, but not those which I've created within my app.

enter image description here

If I get it right, when I take a photo using standart camera it's saved in /storage/emulated/0/DCIM/Camera/. But why when I create photos in this folder manually they are not discovered by any apps?

Code for taking a photo from my app:

public class TakePhotoActivity extends AppCompatActivity {

    Button takePicBtn;
    ImageView takenImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.take_photo);

        takePicBtn = findViewById(R.id.btnTakePic);
        if (Build.VERSION.SDK_INT >= 23){
            requestPermissions(new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
        }
        takePicBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dispatchTakePictureIntent();
                galleryAddPic();
            }
        });
        takenImageView = findViewById(R.id.takenImage);

    }

        static final int REQUEST_IMAGE_CAPTURE = 1;

        @Override
        protected void onActivityResult ( int requestCode, int resultCode, Intent data){
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {


                Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
                takenImageView.setImageBitmap(bitmap);

            }
        }

        String mCurrentPhotoPath;

        private File createImageFile () throws IOException {

            String name = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
            File storageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "Camera");

            File image = File.createTempFile(
                    "IMG_" + name,  /* prefix */
                    ".jpeg",         /* suffix */
                    storageDir      /* directory */
            );
            mCurrentPhotoPath = image.getAbsolutePath();
            return image;
        }

        public void dispatchTakePictureIntent () {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                File photoFile = null;
                try {
                    photoFile = createImageFile();
                } catch (IOException ex) {
                    ex.printStackTrace();
                }
                if (photoFile != null) {

                    System.out.println(BuildConfig.APPLICATION_ID  + ".fileprovider");
                    System.out.println(FileProvider.getUriForFile(TakePhotoActivity.this, BuildConfig.APPLICATION_ID  + ".fileprovider", photoFile));


                    takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, FileProvider.getUriForFile(TakePhotoActivity.this, BuildConfig.APPLICATION_ID  + ".fileprovider", photoFile));
                    startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                }
            }
        }

        private void galleryAddPic () {
            Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
            mediaScanIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            mediaScanIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            File f = new File(mCurrentPhotoPath);
            System.out.println(mCurrentPhotoPath);
            Uri contentUri = FileProvider.getUriForFile(TakePhotoActivity.this, BuildConfig.APPLICATION_ID  + ".fileprovider", f);
            mediaScanIntent.setData(contentUri);
            this.sendBroadcast(mediaScanIntent)
              }
        }
    }

Part of the manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.stpmp">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-feature android:name="android.hardware.camera"
        android:required="true" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/file_paths"></meta-data>
        </provider>

file_paths.xml:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="." />
</paths>

Thanks in advance!

INDRAJITH EKANAYAKE
  • 3,894
  • 11
  • 41
  • 63
Coffemanz
  • 863
  • 2
  • 8
  • 17
  • 1
    Possible duplicate of [How to access an image from the phone's photo gallery?](https://stackoverflow.com/questions/11144783/how-to-access-an-image-from-the-phones-photo-gallery) – Daniel Beleza Apr 12 '19 at 23:42
  • @DanielBeleza Sorry, but I didn't find an answer to my question there – Coffemanz Apr 12 '19 at 23:51
  • 1
    If i remember well, you need to do a rescan of images within your device. For example, if you restart your device, do these photos still not appear? If they do, somehow your code needs to rescan the device before looking for the images – tendai Apr 13 '19 at 00:06
  • @T.Mas I restarted the emulator and it actually worked: I can see all these photos in other apps now. But how can I perfrom this rescan in my code? Thank you. – Coffemanz Apr 13 '19 at 00:13
  • I think that is what you are looking for https://stackoverflow.com/questions/43341614/update-gallery-after-capture-an-image-android – LatheElHafy Apr 13 '19 at 02:41

0 Answers0