How to write file into DCIM directory exactly where camera does?

11,962

Solution 1

You cannot write exactly into the same folder as the default camera app does. But you can make use of Environment.getExternalStoragePublicDirectory(Environment.DI‌​‌RECTORY_DCIM)


mediaStorageDir = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, 
     Uri.fromFile(mediaStorageDir));
startActivityForResult(takePictureFromCameraIntent, 100);

But, please note that this will create only a sub-folder in the DCIM directory and will not store exactly where the default camera does. But you can always create sub-folders with your required folder name.

Solution 2

How would I accomplish this?

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "You Dir. Name";

if you append any string to end this

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

it will create dir inside the folder

How to know the location of camera files?

By default camera uses this location

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)

How to know current naming convention?

IMG_yyyyMMDD_timeStamp

How to gain permissions to that directory?

Using permission manager for Camera and External Storage permissions

Solution 3

Things to keep in mind for this answer:

  • Every phone producer creates it's own Camera App, tailored to their hardware.

  • With the right permissions, App's can write (almost) everywhere they want...

Now to your question(s):

First, we don't know where the photo's are stored, and what the naming convention is. Every one and their mother has a different idea about what is best. So no "Hey it's always there".

Those who seek will find: get read & write permissions for the whole device. With that search the whole device to find in what folders the images are in. Now subtract the folders that come from "social media". My guess would be that the folder with the most and or latest images is the one that you want. To be sure, you will need testers, that trust you.

All that are found were not unorganized: just find the pattern used. There may be a standard for this. The big companies will surely have one. You can ask the device what maker it has. Note, that that answer might not be correct.

Ain't that a picture, no, it's a photo: And then you get the fun part of accessing the camera. Good times. Remember: request picture => get location of picture in memory => save picture as file. Best part, there is no enforcement of all parts of this API, so different devices need different instructions...

Have fun & Good luck!

ps, the downvotes are probably for the lack of code

Share:
11,962

Related videos on Youtube

Dims
Author by

Dims

Software developer & Machine Learning engineer C/C++/Java/C#/Python/Mathematica/MATLAB/Kotlin/R/PHP/JavaScript/SQL/HTML/ LinkedIn: http://www.linkedin.com/in/dimskraft Telegram: https://t.me/dims12 I prefer fishing rod over fish.

Updated on September 15, 2022

Comments

  • Dims
    Dims over 1 year

    Suppose I am writing an alternative Camera application and wish to write images exactly into the same place as Camera does and name them exactly in the same name Camera does.

    How would I accomplish this?

    How to know the location of camera files?

    How to know current naming convention?

    How to gain permissions to that directory?

    Any of answer would be appreciated.


    Okay, suppose it is not really camera alternative. Suppose I would like to write formats other than images, like audio, video, or something else.

    • Jon Adams
      Jon Adams about 7 years
      A couple things: You have a very open ended question. It contains multiple questions instead of a single one that can be answered. There are lots of guides on the internet for more of those tasks—which ones have you tried and are having problems with? And questions for suggestions on libraries or tutorials are discouraged on this site since they can't be answered definitively.
    • CommonsWare
      CommonsWare about 7 years
      There are ~2 billion Android devices, from thousands of models. There are hundreds of different camera apps that ship by default on those models, and there are hundreds of additional camera apps available for download from the Play Store and elsewhere. There is no single "Camera application". You are welcome to use Environment.getExternalStoragePublicDirectory(Environment.DI‌​RECTORY_DCIM) to get the general location, but there is no requirement for any given camera app to put its photos there or name them with some standard format.
    • Alex Cohn
      Alex Cohn about 7 years
      You can learn a lot from OpenCamera project on GitHub
    • CommonsWare
      CommonsWare about 7 years
      "you will see the VERY ONE DCIM folder" -- correct. That is Environment.getExternalStoragePublicDirectory(Environment.DI‌​‌​RECTORY_DCIM). Whether any given camera app writes to that directory, a subdirectory of DCIM, or somewhere else, is up to the developers of the camera app. "I wan't my application write namely here" -- not according to your question, which has "wish to write images exactly into the same place as Camera does and name them exactly in the same name Camera does", for some unidentified "Camera" app.
  • Dims
    Dims about 7 years
    Firs, your suggestion only works if Camera is set up to write to internal storage. If it is set up to write to SD card, your path will be irrelevant. Second, I don't need to create folders, I need to write exactly the same place as camera does. This is the question.
  • Sanal Varghese
    Sanal Varghese about 7 years
    As far as I know, you can't write to exactly where the camera does.
  • Vinayagam.D
    Vinayagam.D about 7 years
    Try Using FileProvider for storing image anywhere you want from android n onwards [Fileprovider] developer.android.com/reference/android/support/v4/content/…
  • Dims
    Dims about 7 years
    @AnujJPandey your code writes to /storage/emulated/0/Pictures/, while I need to write to /storage/9C33-6BBD/DCIM/Camera
  • Dims
    Dims about 7 years
    No, downvotes are from proponents of unfail competition. They displike if people want to do their applications allowed to do
  • Anuj J Pandey
    Anuj J Pandey about 7 years
    ContentValues values = new ContentValues(); values.put(MediaStore.Images.ImageColumns.DATA, data); Uri uri = resolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI‌​, values);